<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - Lua 5.4.0-alpha - src/lgc.c</title>
  <link rel="stylesheet" type="text/css" href="../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../index.html">top level</a> - <a href="index.html">src</a> - lgc.c</td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">Lua 5.4.0-alpha</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">622</td>
            <td class="headerCovTableEntry">734</td>
            <td class="headerCovTableEntryMed">84.7 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2019-06-13 19:44:19</td>
            <td></td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<span class="lineNum">       1 </span>            : /*
<span class="lineNum">       2 </span>            : ** $Id: lgc.c $
<span class="lineNum">       3 </span>            : ** Garbage Collector
<span class="lineNum">       4 </span>            : ** See Copyright Notice in lua.h
<span class="lineNum">       5 </span>            : */
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : #define lgc_c
<span class="lineNum">       8 </span>            : #define LUA_CORE
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span>            : #include &quot;lprefix.h&quot;
<span class="lineNum">      11 </span>            : 
<span class="lineNum">      12 </span>            : #include &lt;stdio.h&gt;
<span class="lineNum">      13 </span>            : #include &lt;string.h&gt;
<span class="lineNum">      14 </span>            : 
<span class="lineNum">      15 </span>            : 
<span class="lineNum">      16 </span>            : #include &quot;lua.h&quot;
<span class="lineNum">      17 </span>            : 
<span class="lineNum">      18 </span>            : #include &quot;ldebug.h&quot;
<span class="lineNum">      19 </span>            : #include &quot;ldo.h&quot;
<span class="lineNum">      20 </span>            : #include &quot;lfunc.h&quot;
<span class="lineNum">      21 </span>            : #include &quot;lgc.h&quot;
<span class="lineNum">      22 </span>            : #include &quot;lmem.h&quot;
<span class="lineNum">      23 </span>            : #include &quot;lobject.h&quot;
<span class="lineNum">      24 </span>            : #include &quot;lstate.h&quot;
<span class="lineNum">      25 </span>            : #include &quot;lstring.h&quot;
<span class="lineNum">      26 </span>            : #include &quot;ltable.h&quot;
<span class="lineNum">      27 </span>            : #include &quot;ltm.h&quot;
<span class="lineNum">      28 </span>            : 
<span class="lineNum">      29 </span>            : 
<span class="lineNum">      30 </span>            : /*
<span class="lineNum">      31 </span>            : ** Maximum number of elements to sweep in each single step.
<span class="lineNum">      32 </span>            : ** (Large enough to dissipate fixed overheads but small enough
<span class="lineNum">      33 </span>            : ** to allow small steps for the collector.)
<span class="lineNum">      34 </span>            : */
<span class="lineNum">      35 </span>            : #define GCSWEEPMAX      100
<span class="lineNum">      36 </span>            : 
<span class="lineNum">      37 </span>            : /*
<span class="lineNum">      38 </span>            : ** Maximum number of finalizers to call in each single step.
<span class="lineNum">      39 </span>            : */
<span class="lineNum">      40 </span>            : #define GCFINMAX        10
<span class="lineNum">      41 </span>            : 
<span class="lineNum">      42 </span>            : 
<span class="lineNum">      43 </span>            : /*
<span class="lineNum">      44 </span>            : ** Cost of calling one finalizer.
<span class="lineNum">      45 </span>            : */
<span class="lineNum">      46 </span>            : #define GCFINALIZECOST  50
<span class="lineNum">      47 </span>            : 
<span class="lineNum">      48 </span>            : 
<span class="lineNum">      49 </span>            : /*
<span class="lineNum">      50 </span>            : ** The equivalent, in bytes, of one unit of &quot;work&quot; (visiting a slot,
<span class="lineNum">      51 </span>            : ** sweeping an object, etc.)
<span class="lineNum">      52 </span>            : */
<span class="lineNum">      53 </span>            : #define WORK2MEM        sizeof(TValue)
<span class="lineNum">      54 </span>            : 
<span class="lineNum">      55 </span>            : 
<span class="lineNum">      56 </span>            : /*
<span class="lineNum">      57 </span>            : ** macro to adjust 'pause': 'pause' is actually used like
<span class="lineNum">      58 </span>            : ** 'pause / PAUSEADJ' (value chosen by tests)
<span class="lineNum">      59 </span>            : */
<span class="lineNum">      60 </span>            : #define PAUSEADJ                100
<span class="lineNum">      61 </span>            : 
<span class="lineNum">      62 </span>            : 
<span class="lineNum">      63 </span>            : /* mask to erase all color bits (plus gen. related stuff) */
<span class="lineNum">      64 </span>            : #define maskcolors      (~(bitmask(BLACKBIT) | WHITEBITS | AGEBITS))
<span class="lineNum">      65 </span>            : 
<span class="lineNum">      66 </span>            : 
<span class="lineNum">      67 </span>            : /* macro to erase all color bits then sets only the current white bit */
<span class="lineNum">      68 </span>            : #define makewhite(g,x)  \
<span class="lineNum">      69 </span>            :  (x-&gt;marked = cast_byte((x-&gt;marked &amp; maskcolors) | luaC_white(g)))
<span class="lineNum">      70 </span>            : 
<span class="lineNum">      71 </span>            : #define white2gray(x)   resetbits(x-&gt;marked, WHITEBITS)
<span class="lineNum">      72 </span>            : #define black2gray(x)   resetbit(x-&gt;marked, BLACKBIT)
<span class="lineNum">      73 </span>            : 
<span class="lineNum">      74 </span>            : 
<span class="lineNum">      75 </span>            : #define valiswhite(x)   (iscollectable(x) &amp;&amp; iswhite(gcvalue(x)))
<span class="lineNum">      76 </span>            : 
<span class="lineNum">      77 </span>            : #define keyiswhite(n)   (keyiscollectable(n) &amp;&amp; iswhite(gckey(n)))
<span class="lineNum">      78 </span>            : 
<span class="lineNum">      79 </span>            : 
<span class="lineNum">      80 </span>            : #define checkconsistency(obj)  \
<span class="lineNum">      81 </span>            :   lua_longassert(!iscollectable(obj) || righttt(obj))
<span class="lineNum">      82 </span>            : 
<span class="lineNum">      83 </span>            : /*
<span class="lineNum">      84 </span>            : ** Protected access to objects in values
<span class="lineNum">      85 </span>            : */
<span class="lineNum">      86 </span>            : #define gcvalueN(o)     (iscollectable(o) ? gcvalue(o) : NULL)
<span class="lineNum">      87 </span>            : 
<span class="lineNum">      88 </span>            : 
<span class="lineNum">      89 </span>            : #define markvalue(g,o) { checkconsistency(o); \
<span class="lineNum">      90 </span>            :   if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); }
<span class="lineNum">      91 </span>            : 
<span class="lineNum">      92 </span>            : #define markkey(g, n)   { if keyiswhite(n) reallymarkobject(g,gckey(n)); }
<span class="lineNum">      93 </span>            : 
<span class="lineNum">      94 </span>            : #define markobject(g,t) { if (iswhite(t)) reallymarkobject(g, obj2gco(t)); }
<span class="lineNum">      95 </span>            : 
<span class="lineNum">      96 </span>            : /*
<span class="lineNum">      97 </span>            : ** mark an object that can be NULL (either because it is really optional,
<span class="lineNum">      98 </span>            : ** or it was stripped as debug info, or inside an uncompleted structure)
<span class="lineNum">      99 </span>            : */
<span class="lineNum">     100 </span>            : #define markobjectN(g,t)        { if (t) markobject(g,t); }
<span class="lineNum">     101 </span>            : 
<span class="lineNum">     102 </span>            : static void reallymarkobject (global_State *g, GCObject *o);
<span class="lineNum">     103 </span>            : static lu_mem atomic (lua_State *L);
<span class="lineNum">     104 </span>            : static void entersweep (lua_State *L);
<span class="lineNum">     105 </span>            : 
<span class="lineNum">     106 </span>            : 
<span class="lineNum">     107 </span>            : /*
<span class="lineNum">     108 </span>            : ** {======================================================
<span class="lineNum">     109 </span>            : ** Generic functions
<span class="lineNum">     110 </span>            : ** =======================================================
<span class="lineNum">     111 </span>            : */
<span class="lineNum">     112 </span>            : 
<span class="lineNum">     113 </span>            : 
<span class="lineNum">     114 </span>            : /*
<span class="lineNum">     115 </span>            : ** one after last element in a hash array
<span class="lineNum">     116 </span>            : */
<span class="lineNum">     117 </span>            : #define gnodelast(h)    gnode(h, cast_sizet(sizenode(h)))
<span class="lineNum">     118 </span>            : 
<span class="lineNum">     119 </span>            : 
<span class="lineNum">     120 </span><span class="lineCov">      25852 : static GCObject **getgclist (GCObject *o) {</span>
<span class="lineNum">     121 </span><span class="lineCov">      25852 :   switch (o-&gt;tt) {</span>
<span class="lineNum">     122 </span><span class="lineCov">      12573 :     case LUA_TTABLE: return &amp;gco2t(o)-&gt;gclist;</span>
<span class="lineNum">     123 </span><span class="lineCov">       2130 :     case LUA_TLCL: return &amp;gco2lcl(o)-&gt;gclist;</span>
<span class="lineNum">     124 </span><span class="lineCov">       4250 :     case LUA_TCCL: return &amp;gco2ccl(o)-&gt;gclist;</span>
<span class="lineNum">     125 </span><span class="lineCov">       1531 :     case LUA_TTHREAD: return &amp;gco2th(o)-&gt;gclist;</span>
<span class="lineNum">     126 </span><span class="lineCov">       5368 :     case LUA_TPROTO: return &amp;gco2p(o)-&gt;gclist;</span>
<span class="lineNum">     127 </span><span class="lineNoCov">          0 :     case LUA_TUSERDATA: {</span>
<span class="lineNum">     128 </span><span class="lineNoCov">          0 :       Udata *u = gco2u(o);</span>
<span class="lineNum">     129 </span>            :       lua_assert(u-&gt;nuvalue &gt; 0);
<span class="lineNum">     130 </span><span class="lineNoCov">          0 :       return &amp;u-&gt;gclist;</span>
<span class="lineNum">     131 </span>            :     }
<span class="lineNum">     132 </span><span class="lineNoCov">          0 :     default: lua_assert(0); return 0;</span>
<span class="lineNum">     133 </span>            :   }
<span class="lineNum">     134 </span>            : }
<span class="lineNum">     135 </span>            : 
<span class="lineNum">     136 </span>            : 
<span class="lineNum">     137 </span>            : /*
<span class="lineNum">     138 </span>            : ** Link a collectable object 'o' with a known type into list pointed by 'p'.
<span class="lineNum">     139 </span>            : */
<span class="lineNum">     140 </span>            : #define linkgclist(o,p) ((o)-&gt;gclist = (p), (p) = obj2gco(o))
<span class="lineNum">     141 </span>            : 
<span class="lineNum">     142 </span>            : 
<span class="lineNum">     143 </span>            : /*
<span class="lineNum">     144 </span>            : ** Link a generic collectable object 'o' into list pointed by 'p'.
<span class="lineNum">     145 </span>            : */
<span class="lineNum">     146 </span>            : #define linkobjgclist(o,p) (*getgclist(o) = (p), (p) = obj2gco(o))
<span class="lineNum">     147 </span>            : 
<span class="lineNum">     148 </span>            : 
<span class="lineNum">     149 </span>            : 
<span class="lineNum">     150 </span>            : /*
<span class="lineNum">     151 </span>            : ** Clear keys for empty entries in tables. If entry is empty
<span class="lineNum">     152 </span>            : ** and its key is not marked, mark its entry as dead. This allows the
<span class="lineNum">     153 </span>            : ** collection of the key, but keeps its entry in the table (its removal
<span class="lineNum">     154 </span>            : ** could break a chain). The main feature of a dead key is that it must
<span class="lineNum">     155 </span>            : ** be different from any other value, to do not disturb searches. 
<span class="lineNum">     156 </span>            : ** Other places never manipulate dead keys, because its associated empty
<span class="lineNum">     157 </span>            : ** value is enough to signal that the entry is logically empty.
<span class="lineNum">     158 </span>            : */
<span class="lineNum">     159 </span><span class="lineCov">      30938 : static void clearkey (Node *n) {</span>
<span class="lineNum">     160 </span>            :   lua_assert(isempty(gval(n)));
<span class="lineNum">     161 </span><span class="lineCov">      30938 :   if (keyiswhite(n))</span>
<span class="lineNum">     162 </span><span class="lineNoCov">          0 :     setdeadkey(n);  /* unused and unmarked key; remove it */</span>
<span class="lineNum">     163 </span><span class="lineCov">      30938 : }</span>
<span class="lineNum">     164 </span>            : 
<span class="lineNum">     165 </span>            : 
<span class="lineNum">     166 </span>            : /*
<span class="lineNum">     167 </span>            : ** tells whether a key or value can be cleared from a weak
<span class="lineNum">     168 </span>            : ** table. Non-collectable objects are never removed from weak
<span class="lineNum">     169 </span>            : ** tables. Strings behave as 'values', so are never removed too. for
<span class="lineNum">     170 </span>            : ** other objects: if really collected, cannot keep them; for objects
<span class="lineNum">     171 </span>            : ** being finalized, keep them in keys, but not in values
<span class="lineNum">     172 </span>            : */
<span class="lineNum">     173 </span><span class="lineCov">          2 : static int iscleared (global_State *g, const GCObject *o) {</span>
<span class="lineNum">     174 </span><span class="lineCov">          2 :   if (o == NULL) return 0;  /* non-collectable value */</span>
<span class="lineNum">     175 </span><span class="lineCov">          2 :   else if (novariant(o-&gt;tt) == LUA_TSTRING) {</span>
<span class="lineNum">     176 </span><span class="lineCov">          1 :     markobject(g, o);  /* strings are 'values', so are never weak */</span>
<span class="lineNum">     177 </span><span class="lineCov">          1 :     return 0;</span>
<span class="lineNum">     178 </span>            :   }
<span class="lineNum">     179 </span><span class="lineCov">          1 :   else return iswhite(o);</span>
<span class="lineNum">     180 </span>            : }
<span class="lineNum">     181 </span>            : 
<span class="lineNum">     182 </span>            : 
<span class="lineNum">     183 </span>            : /*
<span class="lineNum">     184 </span>            : ** barrier that moves collector forward, that is, mark the white object
<span class="lineNum">     185 </span>            : ** 'v' being pointed by the black object 'o'. (If in sweep phase, clear
<span class="lineNum">     186 </span>            : ** the black object to white [sweep it] to avoid other barrier calls for
<span class="lineNum">     187 </span>            : ** this same object.) In the generational mode, 'v' must also become
<span class="lineNum">     188 </span>            : ** old, if 'o' is old; however, it cannot be changed directly to OLD,
<span class="lineNum">     189 </span>            : ** because it may still point to non-old objects. So, it is marked as
<span class="lineNum">     190 </span>            : ** OLD0. In the next cycle it will become OLD1, and in the next it
<span class="lineNum">     191 </span>            : ** will finally become OLD (regular old).
<span class="lineNum">     192 </span>            : */
<span class="lineNum">     193 </span><span class="lineCov">       1076 : void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) {</span>
<span class="lineNum">     194 </span><span class="lineCov">       1076 :   global_State *g = G(L);</span>
<span class="lineNum">     195 </span>            :   lua_assert(isblack(o) &amp;&amp; iswhite(v) &amp;&amp; !isdead(g, v) &amp;&amp; !isdead(g, o));
<span class="lineNum">     196 </span><span class="lineCov">       1076 :   if (keepinvariant(g)) {  /* must keep invariant? */</span>
<span class="lineNum">     197 </span><span class="lineCov">       1076 :     reallymarkobject(g, v);  /* restore invariant */</span>
<span class="lineNum">     198 </span><span class="lineCov">       1076 :     if (isold(o)) {</span>
<span class="lineNum">     199 </span>            :       lua_assert(!isold(v));  /* white object could not be old */
<span class="lineNum">     200 </span><span class="lineCov">       1076 :       setage(v, G_OLD0);  /* restore generational invariant */</span>
<span class="lineNum">     201 </span>            :     }
<span class="lineNum">     202 </span>            :   }
<span class="lineNum">     203 </span>            :   else {  /* sweep phase */
<span class="lineNum">     204 </span>            :     lua_assert(issweepphase(g));
<span class="lineNum">     205 </span><span class="lineNoCov">          0 :     makewhite(g, o);  /* mark main obj. as white to avoid other barriers */</span>
<span class="lineNum">     206 </span>            :   }
<span class="lineNum">     207 </span><span class="lineCov">       1076 : }</span>
<span class="lineNum">     208 </span>            : 
<span class="lineNum">     209 </span>            : 
<span class="lineNum">     210 </span>            : /*
<span class="lineNum">     211 </span>            : ** barrier that moves collector backward, that is, mark the black object
<span class="lineNum">     212 </span>            : ** pointing to a white object as gray again.
<span class="lineNum">     213 </span>            : */
<span class="lineNum">     214 </span><span class="lineCov">        194 : void luaC_barrierback_ (lua_State *L, GCObject *o) {</span>
<span class="lineNum">     215 </span><span class="lineCov">        194 :   global_State *g = G(L);</span>
<span class="lineNum">     216 </span>            :   lua_assert(isblack(o) &amp;&amp; !isdead(g, o));
<span class="lineNum">     217 </span>            :   lua_assert(g-&gt;gckind != KGC_GEN || (isold(o) &amp;&amp; getage(o) != G_TOUCHED1));
<span class="lineNum">     218 </span><span class="lineCov">        194 :   if (getage(o) != G_TOUCHED2)  /* not already in gray list? */</span>
<span class="lineNum">     219 </span><span class="lineCov">        178 :     linkobjgclist(o, g-&gt;grayagain);  /* link it in 'grayagain' */</span>
<span class="lineNum">     220 </span><span class="lineCov">        194 :   black2gray(o);  /* make object gray (again) */</span>
<span class="lineNum">     221 </span><span class="lineCov">        194 :   setage(o, G_TOUCHED1);  /* touched in current cycle */</span>
<span class="lineNum">     222 </span><span class="lineCov">        194 : }</span>
<span class="lineNum">     223 </span>            : 
<span class="lineNum">     224 </span>            : 
<span class="lineNum">     225 </span><span class="lineCov">       5292 : void luaC_fix (lua_State *L, GCObject *o) {</span>
<span class="lineNum">     226 </span><span class="lineCov">       5292 :   global_State *g = G(L);</span>
<span class="lineNum">     227 </span>            :   lua_assert(g-&gt;allgc == o);  /* object must be 1st in 'allgc' list! */
<span class="lineNum">     228 </span><span class="lineCov">       5292 :   white2gray(o);  /* they will be gray forever */</span>
<span class="lineNum">     229 </span><span class="lineCov">       5292 :   setage(o, G_OLD);  /* and old forever */</span>
<span class="lineNum">     230 </span><span class="lineCov">       5292 :   g-&gt;allgc = o-&gt;next;  /* remove object from 'allgc' list */</span>
<span class="lineNum">     231 </span><span class="lineCov">       5292 :   o-&gt;next = g-&gt;fixedgc;  /* link it to 'fixedgc' list */</span>
<span class="lineNum">     232 </span><span class="lineCov">       5292 :   g-&gt;fixedgc = o;</span>
<span class="lineNum">     233 </span><span class="lineCov">       5292 : }</span>
<span class="lineNum">     234 </span>            : 
<span class="lineNum">     235 </span>            : 
<span class="lineNum">     236 </span>            : /*
<span class="lineNum">     237 </span>            : ** create a new collectable object (with given type and size) and link
<span class="lineNum">     238 </span>            : ** it to 'allgc' list.
<span class="lineNum">     239 </span>            : */
<span class="lineNum">     240 </span><span class="lineCov">      65534 : GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) {</span>
<span class="lineNum">     241 </span><span class="lineCov">      65534 :   global_State *g = G(L);</span>
<span class="lineNum">     242 </span><span class="lineCov">      65534 :   GCObject *o = cast(GCObject *, luaM_newobject(L, novariant(tt), sz));</span>
<span class="lineNum">     243 </span><span class="lineCov">      65534 :   o-&gt;marked = luaC_white(g);</span>
<span class="lineNum">     244 </span><span class="lineCov">      65534 :   o-&gt;tt = tt;</span>
<span class="lineNum">     245 </span><span class="lineCov">      65534 :   o-&gt;next = g-&gt;allgc;</span>
<span class="lineNum">     246 </span><span class="lineCov">      65534 :   g-&gt;allgc = o;</span>
<span class="lineNum">     247 </span><span class="lineCov">      65534 :   return o;</span>
<span class="lineNum">     248 </span>            : }
<span class="lineNum">     249 </span>            : 
<span class="lineNum">     250 </span>            : /* }====================================================== */
<span class="lineNum">     251 </span>            : 
<span class="lineNum">     252 </span>            : 
<span class="lineNum">     253 </span>            : 
<span class="lineNum">     254 </span>            : /*
<span class="lineNum">     255 </span>            : ** {======================================================
<span class="lineNum">     256 </span>            : ** Mark functions
<span class="lineNum">     257 </span>            : ** =======================================================
<span class="lineNum">     258 </span>            : */
<span class="lineNum">     259 </span>            : 
<span class="lineNum">     260 </span>            : 
<span class="lineNum">     261 </span>            : /*
<span class="lineNum">     262 </span>            : ** Mark an object. Userdata, strings, and closed upvalues are visited
<span class="lineNum">     263 </span>            : ** and turned black here. Other objects are marked gray and added
<span class="lineNum">     264 </span>            : ** to appropriate list to be visited (and turned black) later. (Open
<span class="lineNum">     265 </span>            : ** upvalues are already linked in 'headuv' list. They are kept gray
<span class="lineNum">     266 </span>            : ** to avoid barriers, as their values will be revisited by the thread.)
<span class="lineNum">     267 </span>            : */
<span class="lineNum">     268 </span><span class="lineCov">      73076 : static void reallymarkobject (global_State *g, GCObject *o) {</span>
<span class="lineNum">     269 </span><span class="lineCov">      73076 :   white2gray(o);</span>
<span class="lineNum">     270 </span><span class="lineCov">      73076 :   switch (o-&gt;tt) {</span>
<span class="lineNum">     271 </span><span class="lineCov">      56324 :     case LUA_TSHRSTR:</span>
<span class="lineNum">     272 </span>            :     case LUA_TLNGSTR: {
<span class="lineNum">     273 </span><span class="lineCov">      56324 :       gray2black(o);</span>
<span class="lineNum">     274 </span><span class="lineCov">      56324 :       break;</span>
<span class="lineNum">     275 </span>            :     }
<span class="lineNum">     276 </span><span class="lineCov">       2829 :     case LUA_TUPVAL:</span>
<span class="lineNum">     277 </span>            :     case LUA_TUPVALTBC: {
<span class="lineNum">     278 </span><span class="lineCov">       2829 :       UpVal *uv = gco2upv(o);</span>
<span class="lineNum">     279 </span><span class="lineCov">       2829 :       if (!upisopen(uv))  /* open upvalues are kept gray */</span>
<span class="lineNum">     280 </span><span class="lineCov">       2186 :         gray2black(o);</span>
<span class="lineNum">     281 </span><span class="lineCov">       2829 :       markvalue(g, uv-&gt;v);  /* mark its content */</span>
<span class="lineNum">     282 </span><span class="lineCov">       2829 :       break;</span>
<span class="lineNum">     283 </span>            :     }
<span class="lineNum">     284 </span><span class="lineNoCov">          0 :     case LUA_TUSERDATA: {</span>
<span class="lineNum">     285 </span><span class="lineCov">       1438 :       Udata *u = gco2u(o);</span>
<span class="lineNum">     286 </span><span class="lineCov">       1438 :       if (u-&gt;nuvalue == 0) {  /* no user values? */</span>
<span class="lineNum">     287 </span><span class="lineCov">       1438 :         markobjectN(g, u-&gt;metatable);  /* mark its metatable */</span>
<span class="lineNum">     288 </span><span class="lineCov">       1438 :         gray2black(o);  /* nothing else to mark */</span>
<span class="lineNum">     289 </span><span class="lineCov">       1438 :         break;</span>
<span class="lineNum">     290 </span>            :       }
<span class="lineNum">     291 </span>            :       /* else... */
<span class="lineNum">     292 </span>            :     }  /* FALLTHROUGH */
<span class="lineNum">     293 </span>            :     case LUA_TLCL: case LUA_TCCL: case LUA_TTABLE:
<span class="lineNum">     294 </span>            :     case LUA_TTHREAD: case LUA_TPROTO: {
<span class="lineNum">     295 </span><span class="lineCov">      12485 :       linkobjgclist(o, g-&gt;gray);</span>
<span class="lineNum">     296 </span><span class="lineCov">      12485 :       break;</span>
<span class="lineNum">     297 </span>            :     }
<span class="lineNum">     298 </span><span class="lineNoCov">          0 :     default: lua_assert(0); break;</span>
<span class="lineNum">     299 </span>            :   }
<span class="lineNum">     300 </span><span class="lineCov">      73076 : }</span>
<span class="lineNum">     301 </span>            : 
<span class="lineNum">     302 </span>            : 
<span class="lineNum">     303 </span>            : /*
<span class="lineNum">     304 </span>            : ** mark metamethods for basic types
<span class="lineNum">     305 </span>            : */
<span class="lineNum">     306 </span><span class="lineCov">       1054 : static void markmt (global_State *g) {</span>
<span class="lineNum">     307 </span>            :   int i;
<span class="lineNum">     308 </span><span class="lineCov">      10540 :   for (i=0; i &lt; LUA_NUMTAGS; i++)</span>
<span class="lineNum">     309 </span><span class="lineCov">       9486 :     markobjectN(g, g-&gt;mt[i]);</span>
<span class="lineNum">     310 </span><span class="lineCov">       1054 : }</span>
<span class="lineNum">     311 </span>            : 
<span class="lineNum">     312 </span>            : 
<span class="lineNum">     313 </span>            : /*
<span class="lineNum">     314 </span>            : ** mark all objects in list of being-finalized
<span class="lineNum">     315 </span>            : */
<span class="lineNum">     316 </span><span class="lineCov">       1054 : static lu_mem markbeingfnz (global_State *g) {</span>
<span class="lineNum">     317 </span>            :   GCObject *o;
<span class="lineNum">     318 </span><span class="lineCov">       1054 :   lu_mem count = 0;</span>
<span class="lineNum">     319 </span><span class="lineCov">       1089 :   for (o = g-&gt;tobefnz; o != NULL; o = o-&gt;next) {</span>
<span class="lineNum">     320 </span><span class="lineCov">         35 :     count++;</span>
<span class="lineNum">     321 </span><span class="lineCov">         35 :     markobject(g, o);</span>
<span class="lineNum">     322 </span>            :   }
<span class="lineNum">     323 </span><span class="lineCov">       1054 :   return count;</span>
<span class="lineNum">     324 </span>            : }
<span class="lineNum">     325 </span>            : 
<span class="lineNum">     326 </span>            : 
<span class="lineNum">     327 </span>            : /*
<span class="lineNum">     328 </span>            : ** Mark all values stored in marked open upvalues from non-marked threads.
<span class="lineNum">     329 </span>            : ** (Values from marked threads were already marked when traversing the
<span class="lineNum">     330 </span>            : ** thread.) Remove from the list threads that no longer have upvalues and
<span class="lineNum">     331 </span>            : ** not-marked threads.
<span class="lineNum">     332 </span>            : */
<span class="lineNum">     333 </span><span class="lineCov">        618 : static int remarkupvals (global_State *g) {</span>
<span class="lineNum">     334 </span>            :   lua_State *thread;
<span class="lineNum">     335 </span><span class="lineCov">        618 :   lua_State **p = &amp;g-&gt;twups;</span>
<span class="lineNum">     336 </span><span class="lineCov">        618 :   int work = 0;</span>
<span class="lineNum">     337 </span><span class="lineCov">        787 :   while ((thread = *p) != NULL) {</span>
<span class="lineNum">     338 </span><span class="lineCov">        169 :     work++;</span>
<span class="lineNum">     339 </span>            :     lua_assert(!isblack(thread));  /* threads are never black */
<span class="lineNum">     340 </span><span class="lineCov">        169 :     if (isgray(thread) &amp;&amp; thread-&gt;openupval != NULL)</span>
<span class="lineNum">     341 </span><span class="lineCov">        115 :       p = &amp;thread-&gt;twups;  /* keep marked thread with upvalues in the list */</span>
<span class="lineNum">     342 </span>            :     else {  /* thread is not marked or without upvalues */
<span class="lineNum">     343 </span>            :       UpVal *uv;
<span class="lineNum">     344 </span><span class="lineCov">         54 :       *p = thread-&gt;twups;  /* remove thread from the list */</span>
<span class="lineNum">     345 </span><span class="lineCov">         54 :       thread-&gt;twups = thread;  /* mark that it is out of list */</span>
<span class="lineNum">     346 </span><span class="lineCov">         54 :       for (uv = thread-&gt;openupval; uv != NULL; uv = uv-&gt;u.open.next) {</span>
<span class="lineNum">     347 </span><span class="lineNoCov">          0 :         work++;</span>
<span class="lineNum">     348 </span><span class="lineNoCov">          0 :         if (!iswhite(uv))  /* upvalue already visited? */</span>
<span class="lineNum">     349 </span><span class="lineNoCov">          0 :           markvalue(g, uv-&gt;v);  /* mark its value */</span>
<span class="lineNum">     350 </span>            :       }
<span class="lineNum">     351 </span>            :     }
<span class="lineNum">     352 </span>            :   }
<span class="lineNum">     353 </span><span class="lineCov">        618 :   return work;</span>
<span class="lineNum">     354 </span>            : }
<span class="lineNum">     355 </span>            : 
<span class="lineNum">     356 </span>            : 
<span class="lineNum">     357 </span>            : /*
<span class="lineNum">     358 </span>            : ** mark root set and reset all gray lists, to start a new collection
<span class="lineNum">     359 </span>            : */
<span class="lineNum">     360 </span><span class="lineCov">        436 : static void restartcollection (global_State *g) {</span>
<span class="lineNum">     361 </span><span class="lineCov">        436 :   g-&gt;gray = g-&gt;grayagain = NULL;</span>
<span class="lineNum">     362 </span><span class="lineCov">        436 :   g-&gt;weak = g-&gt;allweak = g-&gt;ephemeron = NULL;</span>
<span class="lineNum">     363 </span><span class="lineCov">        436 :   markobject(g, g-&gt;mainthread);</span>
<span class="lineNum">     364 </span><span class="lineCov">        436 :   markvalue(g, &amp;g-&gt;l_registry);</span>
<span class="lineNum">     365 </span><span class="lineCov">        436 :   markmt(g);</span>
<span class="lineNum">     366 </span><span class="lineCov">        436 :   markbeingfnz(g);  /* mark any finalizing object left from previous cycle */</span>
<span class="lineNum">     367 </span><span class="lineCov">        436 : }</span>
<span class="lineNum">     368 </span>            : 
<span class="lineNum">     369 </span>            : /* }====================================================== */
<span class="lineNum">     370 </span>            : 
<span class="lineNum">     371 </span>            : 
<span class="lineNum">     372 </span>            : /*
<span class="lineNum">     373 </span>            : ** {======================================================
<span class="lineNum">     374 </span>            : ** Traverse functions
<span class="lineNum">     375 </span>            : ** =======================================================
<span class="lineNum">     376 </span>            : */
<span class="lineNum">     377 </span>            : 
<span class="lineNum">     378 </span>            : /*
<span class="lineNum">     379 </span>            : ** Traverse a table with weak values and link it to proper list. During
<span class="lineNum">     380 </span>            : ** propagate phase, keep it in 'grayagain' list, to be revisited in the
<span class="lineNum">     381 </span>            : ** atomic phase. In the atomic phase, if table has any white value,
<span class="lineNum">     382 </span>            : ** put it in 'weak' list, to be cleared.
<span class="lineNum">     383 </span>            : */
<span class="lineNum">     384 </span><span class="lineNoCov">          0 : static void traverseweakvalue (global_State *g, Table *h) {</span>
<span class="lineNum">     385 </span><span class="lineNoCov">          0 :   Node *n, *limit = gnodelast(h);</span>
<span class="lineNum">     386 </span>            :   /* if there is array part, assume it may have white values (it is not
<span class="lineNum">     387 </span>            :      worth traversing it now just to check) */
<span class="lineNum">     388 </span><span class="lineNoCov">          0 :   int hasclears = (h-&gt;alimit &gt; 0);</span>
<span class="lineNum">     389 </span><span class="lineNoCov">          0 :   for (n = gnode(h, 0); n &lt; limit; n++) {  /* traverse hash part */</span>
<span class="lineNum">     390 </span><span class="lineNoCov">          0 :     if (isempty(gval(n)))  /* entry is empty? */</span>
<span class="lineNum">     391 </span><span class="lineNoCov">          0 :       clearkey(n);  /* clear its key */</span>
<span class="lineNum">     392 </span>            :     else {
<span class="lineNum">     393 </span>            :       lua_assert(!keyisnil(n));
<span class="lineNum">     394 </span><span class="lineNoCov">          0 :       markkey(g, n);</span>
<span class="lineNum">     395 </span><span class="lineNoCov">          0 :       if (!hasclears &amp;&amp; iscleared(g, gcvalueN(gval(n))))  /* a white value? */</span>
<span class="lineNum">     396 </span><span class="lineNoCov">          0 :         hasclears = 1;  /* table will have to be cleared */</span>
<span class="lineNum">     397 </span>            :     }
<span class="lineNum">     398 </span>            :   }
<span class="lineNum">     399 </span><span class="lineNoCov">          0 :   if (g-&gt;gcstate == GCSatomic &amp;&amp; hasclears)</span>
<span class="lineNum">     400 </span><span class="lineNoCov">          0 :     linkgclist(h, g-&gt;weak);  /* has to be cleared later */</span>
<span class="lineNum">     401 </span>            :   else
<span class="lineNum">     402 </span><span class="lineNoCov">          0 :     linkgclist(h, g-&gt;grayagain);  /* must retraverse it in atomic phase */</span>
<span class="lineNum">     403 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">     404 </span>            : 
<span class="lineNum">     405 </span>            : 
<span class="lineNum">     406 </span>            : /*
<span class="lineNum">     407 </span>            : ** Traverse an ephemeron table and link it to proper list. Returns true
<span class="lineNum">     408 </span>            : ** iff any object was marked during this traversal (which implies that
<span class="lineNum">     409 </span>            : ** convergence has to continue). During propagation phase, keep table
<span class="lineNum">     410 </span>            : ** in 'grayagain' list, to be visited again in the atomic phase. In
<span class="lineNum">     411 </span>            : ** the atomic phase, if table has any white-&gt;white entry, it has to
<span class="lineNum">     412 </span>            : ** be revisited during ephemeron convergence (as that key may turn
<span class="lineNum">     413 </span>            : ** black). Otherwise, if it has any white key, table has to be cleared
<span class="lineNum">     414 </span>            : ** (in the atomic phase). In generational mode, it (like all visited
<span class="lineNum">     415 </span>            : ** tables) must be kept in some gray list for post-processing.
<span class="lineNum">     416 </span>            : */
<span class="lineNum">     417 </span><span class="lineCov">          1 : static int traverseephemeron (global_State *g, Table *h) {</span>
<span class="lineNum">     418 </span><span class="lineCov">          1 :   int marked = 0;  /* true if an object is marked in this traversal */</span>
<span class="lineNum">     419 </span><span class="lineCov">          1 :   int hasclears = 0;  /* true if table has white keys */</span>
<span class="lineNum">     420 </span><span class="lineCov">          1 :   int hasww = 0;  /* true if table has entry &quot;white-key -&gt; white-value&quot; */</span>
<span class="lineNum">     421 </span><span class="lineCov">          1 :   Node *n, *limit = gnodelast(h);</span>
<span class="lineNum">     422 </span>            :   unsigned int i;
<span class="lineNum">     423 </span><span class="lineCov">          1 :   unsigned int asize = luaH_realasize(h);</span>
<span class="lineNum">     424 </span>            :   /* traverse array part */
<span class="lineNum">     425 </span><span class="lineCov">          1 :   for (i = 0; i &lt; asize; i++) {</span>
<span class="lineNum">     426 </span><span class="lineNoCov">          0 :     if (valiswhite(&amp;h-&gt;array[i])) {</span>
<span class="lineNum">     427 </span><span class="lineNoCov">          0 :       marked = 1;</span>
<span class="lineNum">     428 </span><span class="lineNoCov">          0 :       reallymarkobject(g, gcvalue(&amp;h-&gt;array[i]));</span>
<span class="lineNum">     429 </span>            :     }
<span class="lineNum">     430 </span>            :   }
<span class="lineNum">     431 </span>            :   /* traverse hash part */
<span class="lineNum">     432 </span><span class="lineCov">          3 :   for (n = gnode(h, 0); n &lt; limit; n++) {</span>
<span class="lineNum">     433 </span><span class="lineCov">          2 :     if (isempty(gval(n)))  /* entry is empty? */</span>
<span class="lineNum">     434 </span><span class="lineNoCov">          0 :       clearkey(n);  /* clear its key */</span>
<span class="lineNum">     435 </span><span class="lineCov">          2 :     else if (iscleared(g, gckeyN(n))) {  /* key is not marked (yet)? */</span>
<span class="lineNum">     436 </span><span class="lineNoCov">          0 :       hasclears = 1;  /* table must be cleared */</span>
<span class="lineNum">     437 </span><span class="lineNoCov">          0 :       if (valiswhite(gval(n)))  /* value not marked yet? */</span>
<span class="lineNum">     438 </span><span class="lineNoCov">          0 :         hasww = 1;  /* white-white entry */</span>
<span class="lineNum">     439 </span>            :     }
<span class="lineNum">     440 </span><span class="lineCov">          2 :     else if (valiswhite(gval(n))) {  /* value not marked yet? */</span>
<span class="lineNum">     441 </span><span class="lineCov">          2 :       marked = 1;</span>
<span class="lineNum">     442 </span><span class="lineCov">          2 :       reallymarkobject(g, gcvalue(gval(n)));  /* mark it now */</span>
<span class="lineNum">     443 </span>            :     }
<span class="lineNum">     444 </span>            :   }
<span class="lineNum">     445 </span>            :   /* link table into proper list */
<span class="lineNum">     446 </span><span class="lineCov">          1 :   if (g-&gt;gcstate == GCSpropagate)</span>
<span class="lineNum">     447 </span><span class="lineNoCov">          0 :     linkgclist(h, g-&gt;grayagain);  /* must retraverse it in atomic phase */</span>
<span class="lineNum">     448 </span><span class="lineCov">          1 :   else if (hasww)  /* table has white-&gt;white entries? */</span>
<span class="lineNum">     449 </span><span class="lineNoCov">          0 :     linkgclist(h, g-&gt;ephemeron);  /* have to propagate again */</span>
<span class="lineNum">     450 </span><span class="lineCov">          1 :   else if (hasclears)  /* table has white keys? */</span>
<span class="lineNum">     451 </span><span class="lineNoCov">          0 :     linkgclist(h, g-&gt;allweak);  /* may have to clean white keys */</span>
<span class="lineNum">     452 </span><span class="lineCov">          1 :   else if (g-&gt;gckind == KGC_GEN)</span>
<span class="lineNum">     453 </span><span class="lineNoCov">          0 :     linkgclist(h, g-&gt;grayagain);  /* keep it in some list */</span>
<span class="lineNum">     454 </span>            :   else
<span class="lineNum">     455 </span><span class="lineCov">          1 :     gray2black(h);</span>
<span class="lineNum">     456 </span><span class="lineCov">          1 :   return marked;</span>
<span class="lineNum">     457 </span>            : }
<span class="lineNum">     458 </span>            : 
<span class="lineNum">     459 </span>            : 
<span class="lineNum">     460 </span><span class="lineCov">       6101 : static void traversestrongtable (global_State *g, Table *h) {</span>
<span class="lineNum">     461 </span><span class="lineCov">       6101 :   Node *n, *limit = gnodelast(h);</span>
<span class="lineNum">     462 </span>            :   unsigned int i;
<span class="lineNum">     463 </span><span class="lineCov">       6101 :   unsigned int asize = luaH_realasize(h);</span>
<span class="lineNum">     464 </span><span class="lineCov">       9212 :   for (i = 0; i &lt; asize; i++)  /* traverse array part */</span>
<span class="lineNum">     465 </span><span class="lineCov">       3111 :     markvalue(g, &amp;h-&gt;array[i]);</span>
<span class="lineNum">     466 </span><span class="lineCov">      97370 :   for (n = gnode(h, 0); n &lt; limit; n++) {  /* traverse hash part */</span>
<span class="lineNum">     467 </span><span class="lineCov">      91269 :     if (isempty(gval(n)))  /* entry is empty? */</span>
<span class="lineNum">     468 </span><span class="lineCov">      30938 :       clearkey(n);  /* clear its key */</span>
<span class="lineNum">     469 </span>            :     else {
<span class="lineNum">     470 </span>            :       lua_assert(!keyisnil(n));
<span class="lineNum">     471 </span><span class="lineCov">      60331 :       markkey(g, n);</span>
<span class="lineNum">     472 </span><span class="lineCov">      60331 :       markvalue(g, gval(n));</span>
<span class="lineNum">     473 </span>            :     }
<span class="lineNum">     474 </span>            :   }
<span class="lineNum">     475 </span><span class="lineCov">       6101 :   if (g-&gt;gckind == KGC_GEN) {</span>
<span class="lineNum">     476 </span><span class="lineCov">        251 :     linkgclist(h, g-&gt;grayagain);  /* keep it in some gray list */</span>
<span class="lineNum">     477 </span><span class="lineCov">        251 :     black2gray(h);</span>
<span class="lineNum">     478 </span>            :   }
<span class="lineNum">     479 </span><span class="lineCov">       6101 : }</span>
<span class="lineNum">     480 </span>            : 
<span class="lineNum">     481 </span>            : 
<span class="lineNum">     482 </span><span class="lineCov">       6102 : static lu_mem traversetable (global_State *g, Table *h) {</span>
<span class="lineNum">     483 </span>            :   const char *weakkey, *weakvalue;
<span class="lineNum">     484 </span><span class="lineCov">       6102 :   const TValue *mode = gfasttm(g, h-&gt;metatable, TM_MODE);</span>
<span class="lineNum">     485 </span><span class="lineCov">       6102 :   markobjectN(g, h-&gt;metatable);</span>
<span class="lineNum">     486 </span><span class="lineCov">       6103 :   if (mode &amp;&amp; ttisstring(mode) &amp;&amp;  /* is there a weak mode? */</span>
<span class="lineNum">     487 </span><span class="lineCov">          1 :       (cast_void(weakkey = strchr(svalue(mode), 'k')),</span>
<span class="lineNum">     488 </span><span class="lineCov">          1 :        cast_void(weakvalue = strchr(svalue(mode), 'v')),</span>
<span class="lineNum">     489 </span><span class="lineCov">          1 :        (weakkey || weakvalue))) {  /* is really weak? */</span>
<span class="lineNum">     490 </span><span class="lineCov">          1 :     black2gray(h);  /* keep table gray */</span>
<span class="lineNum">     491 </span><span class="lineCov">          2 :     if (!weakkey)  /* strong keys? */</span>
<span class="lineNum">     492 </span><span class="lineNoCov">          0 :       traverseweakvalue(g, h);</span>
<span class="lineNum">     493 </span><span class="lineCov">          1 :     else if (!weakvalue)  /* strong values? */</span>
<span class="lineNum">     494 </span><span class="lineCov">          1 :       traverseephemeron(g, h);</span>
<span class="lineNum">     495 </span>            :     else  /* all weak */
<span class="lineNum">     496 </span><span class="lineNoCov">          0 :       linkgclist(h, g-&gt;allweak);  /* nothing to traverse now */</span>
<span class="lineNum">     497 </span>            :   }
<span class="lineNum">     498 </span>            :   else  /* not weak */
<span class="lineNum">     499 </span><span class="lineCov">       6101 :     traversestrongtable(g, h);</span>
<span class="lineNum">     500 </span><span class="lineCov">       6102 :   return 1 + h-&gt;alimit + 2 * allocsizenode(h);</span>
<span class="lineNum">     501 </span>            : }
<span class="lineNum">     502 </span>            : 
<span class="lineNum">     503 </span>            : 
<span class="lineNum">     504 </span><span class="lineNoCov">          0 : static int traverseudata (global_State *g, Udata *u) {</span>
<span class="lineNum">     505 </span>            :   int i;
<span class="lineNum">     506 </span><span class="lineNoCov">          0 :   markobjectN(g, u-&gt;metatable);  /* mark its metatable */</span>
<span class="lineNum">     507 </span><span class="lineNoCov">          0 :   for (i = 0; i &lt; u-&gt;nuvalue; i++)</span>
<span class="lineNum">     508 </span><span class="lineNoCov">          0 :     markvalue(g, &amp;u-&gt;uv[i].uv);</span>
<span class="lineNum">     509 </span><span class="lineNoCov">          0 :   if (g-&gt;gckind == KGC_GEN) {</span>
<span class="lineNum">     510 </span><span class="lineNoCov">          0 :     linkgclist(u, g-&gt;grayagain);  /* keep it in some gray list */</span>
<span class="lineNum">     511 </span><span class="lineNoCov">          0 :     black2gray(u);</span>
<span class="lineNum">     512 </span>            :   }
<span class="lineNum">     513 </span><span class="lineNoCov">          0 :   return 1 + u-&gt;nuvalue;</span>
<span class="lineNum">     514 </span>            : }
<span class="lineNum">     515 </span>            : 
<span class="lineNum">     516 </span>            : 
<span class="lineNum">     517 </span>            : /*
<span class="lineNum">     518 </span>            : ** Traverse a prototype. (While a prototype is being build, its
<span class="lineNum">     519 </span>            : ** arrays can be larger than needed; the extra slots are filled with
<span class="lineNum">     520 </span>            : ** NULL, so the use of 'markobjectN')
<span class="lineNum">     521 </span>            : */
<span class="lineNum">     522 </span><span class="lineCov">       2555 : static int traverseproto (global_State *g, Proto *f) {</span>
<span class="lineNum">     523 </span>            :   int i;
<span class="lineNum">     524 </span><span class="lineCov">       2555 :   markobjectN(g, f-&gt;source);</span>
<span class="lineNum">     525 </span><span class="lineCov">      17369 :   for (i = 0; i &lt; f-&gt;sizek; i++)  /* mark literals */</span>
<span class="lineNum">     526 </span><span class="lineCov">      14814 :     markvalue(g, &amp;f-&gt;k[i]);</span>
<span class="lineNum">     527 </span><span class="lineCov">       6616 :   for (i = 0; i &lt; f-&gt;sizeupvalues; i++)  /* mark upvalue names */</span>
<span class="lineNum">     528 </span><span class="lineCov">       4061 :     markobjectN(g, f-&gt;upvalues[i].name);</span>
<span class="lineNum">     529 </span><span class="lineCov">       4902 :   for (i = 0; i &lt; f-&gt;sizep; i++)  /* mark nested protos */</span>
<span class="lineNum">     530 </span><span class="lineCov">       2347 :     markobjectN(g, f-&gt;p[i]);</span>
<span class="lineNum">     531 </span><span class="lineCov">       9111 :   for (i = 0; i &lt; f-&gt;sizelocvars; i++)  /* mark local-variable names */</span>
<span class="lineNum">     532 </span><span class="lineCov">       6556 :     markobjectN(g, f-&gt;locvars[i].varname);</span>
<span class="lineNum">     533 </span><span class="lineCov">       2555 :   return 1 + f-&gt;sizek + f-&gt;sizeupvalues + f-&gt;sizep + f-&gt;sizelocvars;</span>
<span class="lineNum">     534 </span>            : }
<span class="lineNum">     535 </span>            : 
<span class="lineNum">     536 </span>            : 
<span class="lineNum">     537 </span><span class="lineCov">       2125 : static int traverseCclosure (global_State *g, CClosure *cl) {</span>
<span class="lineNum">     538 </span>            :   int i;
<span class="lineNum">     539 </span><span class="lineCov">       4264 :   for (i = 0; i &lt; cl-&gt;nupvalues; i++)  /* mark its upvalues */</span>
<span class="lineNum">     540 </span><span class="lineCov">       2139 :     markvalue(g, &amp;cl-&gt;upvalue[i]);</span>
<span class="lineNum">     541 </span><span class="lineCov">       2125 :   return 1 + cl-&gt;nupvalues;</span>
<span class="lineNum">     542 </span>            : }
<span class="lineNum">     543 </span>            : 
<span class="lineNum">     544 </span>            : /*
<span class="lineNum">     545 </span>            : ** Traverse a Lua closure, marking its prototype and its upvalues.
<span class="lineNum">     546 </span>            : ** (Both can be NULL while closure is being created.)
<span class="lineNum">     547 </span>            : */
<span class="lineNum">     548 </span><span class="lineCov">       1065 : static int traverseLclosure (global_State *g, LClosure *cl) {</span>
<span class="lineNum">     549 </span>            :   int i;
<span class="lineNum">     550 </span><span class="lineCov">       1065 :   markobjectN(g, cl-&gt;p);  /* mark its prototype */</span>
<span class="lineNum">     551 </span><span class="lineCov">       5163 :   for (i = 0; i &lt; cl-&gt;nupvalues; i++) {  /* visit its upvalues */</span>
<span class="lineNum">     552 </span><span class="lineCov">       4098 :     UpVal *uv = cl-&gt;upvals[i];</span>
<span class="lineNum">     553 </span><span class="lineCov">       4098 :     markobjectN(g, uv);  /* mark upvalue */</span>
<span class="lineNum">     554 </span>            :   }
<span class="lineNum">     555 </span><span class="lineCov">       1065 :   return 1 + cl-&gt;nupvalues;</span>
<span class="lineNum">     556 </span>            : }
<span class="lineNum">     557 </span>            : 
<span class="lineNum">     558 </span>            : 
<span class="lineNum">     559 </span>            : /*
<span class="lineNum">     560 </span>            : ** Traverse a thread, marking the elements in the stack up to its top
<span class="lineNum">     561 </span>            : ** and cleaning the rest of the stack in the final traversal.
<span class="lineNum">     562 </span>            : ** That ensures that the entire stack have valid (non-dead) objects.
<span class="lineNum">     563 </span>            : */
<span class="lineNum">     564 </span><span class="lineCov">       1091 : static int traversethread (global_State *g, lua_State *th) {</span>
<span class="lineNum">     565 </span>            :   UpVal *uv;
<span class="lineNum">     566 </span><span class="lineCov">       1091 :   StkId o = th-&gt;stack;</span>
<span class="lineNum">     567 </span><span class="lineCov">       1091 :   if (o == NULL)</span>
<span class="lineNum">     568 </span><span class="lineNoCov">          0 :     return 1;  /* stack not completely built yet */</span>
<span class="lineNum">     569 </span>            :   lua_assert(g-&gt;gcstate == GCSatomic ||
<span class="lineNum">     570 </span>            :              th-&gt;openupval == NULL || isintwups(th));
<span class="lineNum">     571 </span><span class="lineCov">      17063 :   for (; o &lt; th-&gt;top; o++)  /* mark live elements in the stack */</span>
<span class="lineNum">     572 </span><span class="lineCov">      15972 :     markvalue(g, s2v(o));</span>
<span class="lineNum">     573 </span><span class="lineCov">       1837 :   for (uv = th-&gt;openupval; uv != NULL; uv = uv-&gt;u.open.next) {</span>
<span class="lineNum">     574 </span><span class="lineCov">        746 :     if (uv-&gt;tt == LUA_TUPVALTBC)  /* to be closed? */</span>
<span class="lineNum">     575 </span><span class="lineCov">          2 :       markobject(g, uv);  /* cannot be collected */</span>
<span class="lineNum">     576 </span>            :   }
<span class="lineNum">     577 </span><span class="lineCov">       1091 :   if (g-&gt;gcstate == GCSatomic) {  /* final traversal? */</span>
<span class="lineNum">     578 </span><span class="lineCov">        805 :     StkId lim = th-&gt;stack + th-&gt;stacksize;  /* real end of stack */</span>
<span class="lineNum">     579 </span><span class="lineCov">      41703 :     for (; o &lt; lim; o++)  /* clear not-marked stack slice */</span>
<span class="lineNum">     580 </span><span class="lineCov">      40898 :       setnilvalue(s2v(o));</span>
<span class="lineNum">     581 </span>            :     /* 'remarkupvals' may have removed thread from 'twups' list */
<span class="lineNum">     582 </span><span class="lineCov">        805 :     if (!isintwups(th) &amp;&amp; th-&gt;openupval != NULL) {</span>
<span class="lineNum">     583 </span><span class="lineNoCov">          0 :       th-&gt;twups = g-&gt;twups;  /* link it back to the list */</span>
<span class="lineNum">     584 </span><span class="lineNoCov">          0 :       g-&gt;twups = th;</span>
<span class="lineNum">     585 </span>            :     }
<span class="lineNum">     586 </span>            :   }
<span class="lineNum">     587 </span><span class="lineCov">        286 :   else if (!g-&gt;gcemergency)</span>
<span class="lineNum">     588 </span><span class="lineCov">        286 :     luaD_shrinkstack(th); /* do not change stack in emergency cycle */</span>
<span class="lineNum">     589 </span><span class="lineCov">       1091 :   return 1 + th-&gt;stacksize;</span>
<span class="lineNum">     590 </span>            : }
<span class="lineNum">     591 </span>            : 
<span class="lineNum">     592 </span>            : 
<span class="lineNum">     593 </span>            : /*
<span class="lineNum">     594 </span>            : ** traverse one gray object, turning it to black (except for threads,
<span class="lineNum">     595 </span>            : ** which are always gray).
<span class="lineNum">     596 </span>            : */
<span class="lineNum">     597 </span><span class="lineCov">      12938 : static lu_mem propagatemark (global_State *g) {</span>
<span class="lineNum">     598 </span><span class="lineCov">      12938 :   GCObject *o = g-&gt;gray;</span>
<span class="lineNum">     599 </span><span class="lineCov">      12938 :   gray2black(o);</span>
<span class="lineNum">     600 </span><span class="lineCov">      12938 :   g-&gt;gray = *getgclist(o);  /* remove from 'gray' list */</span>
<span class="lineNum">     601 </span><span class="lineCov">      12938 :   switch (o-&gt;tt) {</span>
<span class="lineNum">     602 </span><span class="lineCov">       6102 :     case LUA_TTABLE: return traversetable(g, gco2t(o));</span>
<span class="lineNum">     603 </span><span class="lineNoCov">          0 :     case LUA_TUSERDATA: return traverseudata(g, gco2u(o));</span>
<span class="lineNum">     604 </span><span class="lineCov">       1065 :     case LUA_TLCL: return traverseLclosure(g, gco2lcl(o));</span>
<span class="lineNum">     605 </span><span class="lineCov">       2125 :     case LUA_TCCL: return traverseCclosure(g, gco2ccl(o));</span>
<span class="lineNum">     606 </span><span class="lineCov">       2555 :     case LUA_TPROTO: return traverseproto(g, gco2p(o));</span>
<span class="lineNum">     607 </span><span class="lineCov">       1091 :     case LUA_TTHREAD: {</span>
<span class="lineNum">     608 </span><span class="lineCov">       1091 :       lua_State *th = gco2th(o);</span>
<span class="lineNum">     609 </span><span class="lineCov">       1091 :       linkgclist(th, g-&gt;grayagain);  /* insert into 'grayagain' list */</span>
<span class="lineNum">     610 </span><span class="lineCov">       1091 :       black2gray(o);</span>
<span class="lineNum">     611 </span><span class="lineCov">       1091 :       return traversethread(g, th);</span>
<span class="lineNum">     612 </span>            :     }
<span class="lineNum">     613 </span><span class="lineNoCov">          0 :     default: lua_assert(0); return 0;</span>
<span class="lineNum">     614 </span>            :   }
<span class="lineNum">     615 </span>            : }
<span class="lineNum">     616 </span>            : 
<span class="lineNum">     617 </span>            : 
<span class="lineNum">     618 </span><span class="lineCov">       2472 : static lu_mem propagateall (global_State *g) {</span>
<span class="lineNum">     619 </span><span class="lineCov">       2472 :   lu_mem tot = 0;</span>
<span class="lineNum">     620 </span><span class="lineCov">      11170 :   while (g-&gt;gray)</span>
<span class="lineNum">     621 </span><span class="lineCov">       8698 :     tot += propagatemark(g);</span>
<span class="lineNum">     622 </span><span class="lineCov">       2472 :   return tot;</span>
<span class="lineNum">     623 </span>            : }
<span class="lineNum">     624 </span>            : 
<span class="lineNum">     625 </span>            : 
<span class="lineNum">     626 </span><span class="lineCov">       1236 : static void convergeephemerons (global_State *g) {</span>
<span class="lineNum">     627 </span>            :   int changed;
<span class="lineNum">     628 </span>            :   do {
<span class="lineNum">     629 </span>            :     GCObject *w;
<span class="lineNum">     630 </span><span class="lineCov">       1236 :     GCObject *next = g-&gt;ephemeron;  /* get ephemeron list */</span>
<span class="lineNum">     631 </span><span class="lineCov">       1236 :     g-&gt;ephemeron = NULL;  /* tables may return to this list when traversed */</span>
<span class="lineNum">     632 </span><span class="lineCov">       1236 :     changed = 0;</span>
<span class="lineNum">     633 </span><span class="lineCov">       1236 :     while ((w = next) != NULL) {</span>
<span class="lineNum">     634 </span><span class="lineNoCov">          0 :       next = gco2t(w)-&gt;gclist;</span>
<span class="lineNum">     635 </span><span class="lineNoCov">          0 :       if (traverseephemeron(g, gco2t(w))) {  /* traverse marked some value? */</span>
<span class="lineNum">     636 </span><span class="lineNoCov">          0 :         propagateall(g);  /* propagate changes */</span>
<span class="lineNum">     637 </span><span class="lineNoCov">          0 :         changed = 1;  /* will have to revisit all ephemeron tables */</span>
<span class="lineNum">     638 </span>            :       }
<span class="lineNum">     639 </span>            :     }
<span class="lineNum">     640 </span><span class="lineCov">       1236 :   } while (changed);</span>
<span class="lineNum">     641 </span><span class="lineCov">       1236 : }</span>
<span class="lineNum">     642 </span>            : 
<span class="lineNum">     643 </span>            : /* }====================================================== */
<span class="lineNum">     644 </span>            : 
<span class="lineNum">     645 </span>            : 
<span class="lineNum">     646 </span>            : /*
<span class="lineNum">     647 </span>            : ** {======================================================
<span class="lineNum">     648 </span>            : ** Sweep Functions
<span class="lineNum">     649 </span>            : ** =======================================================
<span class="lineNum">     650 </span>            : */
<span class="lineNum">     651 </span>            : 
<span class="lineNum">     652 </span>            : 
<span class="lineNum">     653 </span>            : /*
<span class="lineNum">     654 </span>            : ** clear entries with unmarked keys from all weaktables in list 'l'
<span class="lineNum">     655 </span>            : */
<span class="lineNum">     656 </span><span class="lineCov">       1236 : static void clearbykeys (global_State *g, GCObject *l) {</span>
<span class="lineNum">     657 </span><span class="lineCov">       1236 :   for (; l; l = gco2t(l)-&gt;gclist) {</span>
<span class="lineNum">     658 </span><span class="lineNoCov">          0 :     Table *h = gco2t(l);</span>
<span class="lineNum">     659 </span><span class="lineNoCov">          0 :     Node *limit = gnodelast(h);</span>
<span class="lineNum">     660 </span>            :     Node *n;
<span class="lineNum">     661 </span><span class="lineNoCov">          0 :     for (n = gnode(h, 0); n &lt; limit; n++) {</span>
<span class="lineNum">     662 </span><span class="lineNoCov">          0 :       if (iscleared(g, gckeyN(n)))  /* unmarked key? */</span>
<span class="lineNum">     663 </span><span class="lineNoCov">          0 :         setempty(gval(n));  /* remove entry */</span>
<span class="lineNum">     664 </span><span class="lineNoCov">          0 :       if (isempty(gval(n)))  /* is entry empty? */</span>
<span class="lineNum">     665 </span><span class="lineNoCov">          0 :         clearkey(n);  /* clear its key */</span>
<span class="lineNum">     666 </span>            :     }
<span class="lineNum">     667 </span>            :   }
<span class="lineNum">     668 </span><span class="lineCov">       1236 : }</span>
<span class="lineNum">     669 </span>            : 
<span class="lineNum">     670 </span>            : 
<span class="lineNum">     671 </span>            : /*
<span class="lineNum">     672 </span>            : ** clear entries with unmarked values from all weaktables in list 'l' up
<span class="lineNum">     673 </span>            : ** to element 'f'
<span class="lineNum">     674 </span>            : */
<span class="lineNum">     675 </span><span class="lineCov">       2472 : static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) {</span>
<span class="lineNum">     676 </span><span class="lineCov">       2472 :   for (; l != f; l = gco2t(l)-&gt;gclist) {</span>
<span class="lineNum">     677 </span><span class="lineNoCov">          0 :     Table *h = gco2t(l);</span>
<span class="lineNum">     678 </span><span class="lineNoCov">          0 :     Node *n, *limit = gnodelast(h);</span>
<span class="lineNum">     679 </span>            :     unsigned int i;
<span class="lineNum">     680 </span><span class="lineNoCov">          0 :     unsigned int asize = luaH_realasize(h);</span>
<span class="lineNum">     681 </span><span class="lineNoCov">          0 :     for (i = 0; i &lt; asize; i++) {</span>
<span class="lineNum">     682 </span><span class="lineNoCov">          0 :       TValue *o = &amp;h-&gt;array[i];</span>
<span class="lineNum">     683 </span><span class="lineNoCov">          0 :       if (iscleared(g, gcvalueN(o)))  /* value was collected? */</span>
<span class="lineNum">     684 </span><span class="lineNoCov">          0 :         setempty(o);  /* remove entry */</span>
<span class="lineNum">     685 </span>            :     }
<span class="lineNum">     686 </span><span class="lineNoCov">          0 :     for (n = gnode(h, 0); n &lt; limit; n++) {</span>
<span class="lineNum">     687 </span><span class="lineNoCov">          0 :       if (iscleared(g, gcvalueN(gval(n))))  /* unmarked value? */</span>
<span class="lineNum">     688 </span><span class="lineNoCov">          0 :         setempty(gval(n));  /* remove entry */</span>
<span class="lineNum">     689 </span><span class="lineNoCov">          0 :       if (isempty(gval(n)))  /* is entry empty? */</span>
<span class="lineNum">     690 </span><span class="lineNoCov">          0 :         clearkey(n);  /* clear its key */</span>
<span class="lineNum">     691 </span>            :     }
<span class="lineNum">     692 </span>            :   }
<span class="lineNum">     693 </span><span class="lineCov">       2472 : }</span>
<span class="lineNum">     694 </span>            : 
<span class="lineNum">     695 </span>            : 
<span class="lineNum">     696 </span><span class="lineCov">       5667 : static void freeupval (lua_State *L, UpVal *uv) {</span>
<span class="lineNum">     697 </span><span class="lineCov">       5667 :   if (upisopen(uv))</span>
<span class="lineNum">     698 </span><span class="lineCov">          5 :     luaF_unlinkupval(uv);</span>
<span class="lineNum">     699 </span><span class="lineCov">       5667 :   luaM_free(L, uv);</span>
<span class="lineNum">     700 </span><span class="lineCov">       5667 : }</span>
<span class="lineNum">     701 </span>            : 
<span class="lineNum">     702 </span>            : 
<span class="lineNum">     703 </span><span class="lineCov">      62124 : static void freeobj (lua_State *L, GCObject *o) {</span>
<span class="lineNum">     704 </span><span class="lineCov">      62124 :   switch (o-&gt;tt) {</span>
<span class="lineNum">     705 </span><span class="lineCov">       1965 :     case LUA_TPROTO:</span>
<span class="lineNum">     706 </span><span class="lineCov">       1965 :       luaF_freeproto(L, gco2p(o));</span>
<span class="lineNum">     707 </span><span class="lineCov">       1965 :       break;</span>
<span class="lineNum">     708 </span><span class="lineCov">       5667 :     case LUA_TUPVAL:</span>
<span class="lineNum">     709 </span>            :     case LUA_TUPVALTBC:
<span class="lineNum">     710 </span><span class="lineCov">       5667 :       freeupval(L, gco2upv(o));</span>
<span class="lineNum">     711 </span><span class="lineCov">       5667 :       break;</span>
<span class="lineNum">     712 </span><span class="lineCov">       2811 :     case LUA_TLCL:</span>
<span class="lineNum">     713 </span><span class="lineCov">       2811 :       luaM_freemem(L, o, sizeLclosure(gco2lcl(o)-&gt;nupvalues));</span>
<span class="lineNum">     714 </span><span class="lineCov">       2811 :       break;</span>
<span class="lineNum">     715 </span><span class="lineCov">        580 :     case LUA_TCCL:</span>
<span class="lineNum">     716 </span><span class="lineCov">        580 :       luaM_freemem(L, o, sizeCclosure(gco2ccl(o)-&gt;nupvalues));</span>
<span class="lineNum">     717 </span><span class="lineCov">        580 :       break;</span>
<span class="lineNum">     718 </span><span class="lineCov">       8670 :     case LUA_TTABLE:</span>
<span class="lineNum">     719 </span><span class="lineCov">       8670 :       luaH_free(L, gco2t(o));</span>
<span class="lineNum">     720 </span><span class="lineCov">       8670 :       break;</span>
<span class="lineNum">     721 </span><span class="lineCov">         27 :     case LUA_TTHREAD:</span>
<span class="lineNum">     722 </span><span class="lineCov">         27 :       luaE_freethread(L, gco2th(o));</span>
<span class="lineNum">     723 </span><span class="lineCov">         27 :       break;</span>
<span class="lineNum">     724 </span><span class="lineCov">        542 :     case LUA_TUSERDATA: {</span>
<span class="lineNum">     725 </span><span class="lineCov">        542 :       Udata *u = gco2u(o);</span>
<span class="lineNum">     726 </span><span class="lineCov">        542 :       luaM_freemem(L, o, sizeudata(u-&gt;nuvalue, u-&gt;len));</span>
<span class="lineNum">     727 </span><span class="lineCov">        542 :       break;</span>
<span class="lineNum">     728 </span>            :     }
<span class="lineNum">     729 </span><span class="lineCov">      39242 :     case LUA_TSHRSTR:</span>
<span class="lineNum">     730 </span><span class="lineCov">      39242 :       luaS_remove(L, gco2ts(o));  /* remove it from hash table */</span>
<span class="lineNum">     731 </span><span class="lineCov">      39242 :       luaM_freemem(L, o, sizelstring(gco2ts(o)-&gt;shrlen));</span>
<span class="lineNum">     732 </span><span class="lineCov">      39242 :       break;</span>
<span class="lineNum">     733 </span><span class="lineCov">       2620 :     case LUA_TLNGSTR:</span>
<span class="lineNum">     734 </span><span class="lineCov">       2620 :       luaM_freemem(L, o, sizelstring(gco2ts(o)-&gt;u.lnglen));</span>
<span class="lineNum">     735 </span><span class="lineCov">       2620 :       break;</span>
<span class="lineNum">     736 </span><span class="lineCov">      62124 :     default: lua_assert(0);</span>
<span class="lineNum">     737 </span>            :   }
<span class="lineNum">     738 </span><span class="lineCov">      62124 : }</span>
<span class="lineNum">     739 </span>            : 
<span class="lineNum">     740 </span>            : 
<span class="lineNum">     741 </span>            : /*
<span class="lineNum">     742 </span>            : ** sweep at most 'countin' elements from a list of GCObjects erasing dead
<span class="lineNum">     743 </span>            : ** objects, where a dead object is one marked with the old (non current)
<span class="lineNum">     744 </span>            : ** white; change all non-dead objects back to white, preparing for next
<span class="lineNum">     745 </span>            : ** collection cycle. Return where to continue the traversal or NULL if
<span class="lineNum">     746 </span>            : ** list is finished. ('*countout' gets the number of elements traversed.)
<span class="lineNum">     747 </span>            : */
<span class="lineNum">     748 </span><span class="lineCov">       1338 : static GCObject **sweeplist (lua_State *L, GCObject **p, int countin,</span>
<span class="lineNum">     749 </span>            :                              int *countout) {
<span class="lineNum">     750 </span><span class="lineCov">       1338 :   global_State *g = G(L);</span>
<span class="lineNum">     751 </span><span class="lineCov">       1338 :   int ow = otherwhite(g);</span>
<span class="lineNum">     752 </span>            :   int i;
<span class="lineNum">     753 </span><span class="lineCov">       1338 :   int white = luaC_white(g);  /* current white */</span>
<span class="lineNum">     754 </span><span class="lineCov">      33717 :   for (i = 0; *p != NULL &amp;&amp; i &lt; countin; i++) {</span>
<span class="lineNum">     755 </span><span class="lineCov">      32379 :     GCObject *curr = *p;</span>
<span class="lineNum">     756 </span><span class="lineCov">      32379 :     int marked = curr-&gt;marked;</span>
<span class="lineNum">     757 </span><span class="lineCov">      32379 :     if (isdeadm(ow, marked)) {  /* is 'curr' dead? */</span>
<span class="lineNum">     758 </span><span class="lineCov">       1473 :       *p = curr-&gt;next;  /* remove 'curr' from list */</span>
<span class="lineNum">     759 </span><span class="lineCov">       1473 :       freeobj(L, curr);  /* erase 'curr' */</span>
<span class="lineNum">     760 </span>            :     }
<span class="lineNum">     761 </span>            :     else {  /* change mark to 'white' */
<span class="lineNum">     762 </span><span class="lineCov">      30906 :       curr-&gt;marked = cast_byte((marked &amp; maskcolors) | white);</span>
<span class="lineNum">     763 </span><span class="lineCov">      30906 :       p = &amp;curr-&gt;next;  /* go to next element */</span>
<span class="lineNum">     764 </span>            :     }
<span class="lineNum">     765 </span>            :   }
<span class="lineNum">     766 </span><span class="lineCov">       1338 :   if (countout)</span>
<span class="lineNum">     767 </span><span class="lineCov">       1044 :     *countout = i;  /* number of elements traversed */</span>
<span class="lineNum">     768 </span><span class="lineCov">       1338 :   return (*p == NULL) ? NULL : p;</span>
<span class="lineNum">     769 </span>            : }
<span class="lineNum">     770 </span>            : 
<span class="lineNum">     771 </span>            : 
<span class="lineNum">     772 </span>            : /*
<span class="lineNum">     773 </span>            : ** sweep a list until a live object (or end of list)
<span class="lineNum">     774 </span>            : */
<span class="lineNum">     775 </span><span class="lineCov">        294 : static GCObject **sweeptolive (lua_State *L, GCObject **p) {</span>
<span class="lineNum">     776 </span><span class="lineCov">        294 :   GCObject **old = p;</span>
<span class="lineNum">     777 </span>            :   do {
<span class="lineNum">     778 </span><span class="lineCov">        294 :     p = sweeplist(L, p, 1, NULL);</span>
<span class="lineNum">     779 </span><span class="lineCov">        294 :   } while (p == old);</span>
<span class="lineNum">     780 </span><span class="lineCov">        294 :   return p;</span>
<span class="lineNum">     781 </span>            : }
<span class="lineNum">     782 </span>            : 
<span class="lineNum">     783 </span>            : /* }====================================================== */
<span class="lineNum">     784 </span>            : 
<span class="lineNum">     785 </span>            : 
<span class="lineNum">     786 </span>            : /*
<span class="lineNum">     787 </span>            : ** {======================================================
<span class="lineNum">     788 </span>            : ** Finalization
<span class="lineNum">     789 </span>            : ** =======================================================
<span class="lineNum">     790 </span>            : */
<span class="lineNum">     791 </span>            : 
<span class="lineNum">     792 </span>            : /*
<span class="lineNum">     793 </span>            : ** If possible, shrink string table.
<span class="lineNum">     794 </span>            : */
<span class="lineNum">     795 </span><span class="lineCov">        618 : static void checkSizes (lua_State *L, global_State *g) {</span>
<span class="lineNum">     796 </span><span class="lineCov">        618 :   if (!g-&gt;gcemergency) {</span>
<span class="lineNum">     797 </span><span class="lineCov">        618 :     l_mem olddebt = g-&gt;GCdebt;</span>
<span class="lineNum">     798 </span><span class="lineCov">        618 :     if (g-&gt;strt.nuse &lt; g-&gt;strt.size / 4)  /* string table too big? */</span>
<span class="lineNum">     799 </span><span class="lineNoCov">          0 :       luaS_resize(L, g-&gt;strt.size / 2);</span>
<span class="lineNum">     800 </span><span class="lineCov">        618 :     g-&gt;GCestimate += g-&gt;GCdebt - olddebt;  /* correct estimate */</span>
<span class="lineNum">     801 </span>            :   }
<span class="lineNum">     802 </span><span class="lineCov">        618 : }</span>
<span class="lineNum">     803 </span>            : 
<span class="lineNum">     804 </span>            : 
<span class="lineNum">     805 </span>            : /*
<span class="lineNum">     806 </span>            : ** Get the next udata to be finalized from the 'tobefnz' list, and
<span class="lineNum">     807 </span>            : ** link it back into the 'allgc' list.
<span class="lineNum">     808 </span>            : */
<span class="lineNum">     809 </span><span class="lineCov">        443 : static GCObject *udata2finalize (global_State *g) {</span>
<span class="lineNum">     810 </span><span class="lineCov">        443 :   GCObject *o = g-&gt;tobefnz;  /* get first element */</span>
<span class="lineNum">     811 </span>            :   lua_assert(tofinalize(o));
<span class="lineNum">     812 </span><span class="lineCov">        443 :   g-&gt;tobefnz = o-&gt;next;  /* remove it from 'tobefnz' list */</span>
<span class="lineNum">     813 </span><span class="lineCov">        443 :   o-&gt;next = g-&gt;allgc;  /* return it to 'allgc' list */</span>
<span class="lineNum">     814 </span><span class="lineCov">        443 :   g-&gt;allgc = o;</span>
<span class="lineNum">     815 </span><span class="lineCov">        443 :   resetbit(o-&gt;marked, FINALIZEDBIT);  /* object is &quot;normal&quot; again */</span>
<span class="lineNum">     816 </span><span class="lineCov">        443 :   if (issweepphase(g))</span>
<span class="lineNum">     817 </span><span class="lineNoCov">          0 :     makewhite(g, o);  /* &quot;sweep&quot; object */</span>
<span class="lineNum">     818 </span><span class="lineCov">        443 :   return o;</span>
<span class="lineNum">     819 </span>            : }
<span class="lineNum">     820 </span>            : 
<span class="lineNum">     821 </span>            : 
<span class="lineNum">     822 </span><span class="lineCov">        443 : static void dothecall (lua_State *L, void *ud) {</span>
<span class="lineNum">     823 </span>            :   UNUSED(ud);
<span class="lineNum">     824 </span><span class="lineCov">        443 :   luaD_callnoyield(L, L-&gt;top - 2, 0);</span>
<span class="lineNum">     825 </span><span class="lineCov">        443 : }</span>
<span class="lineNum">     826 </span>            : 
<span class="lineNum">     827 </span>            : 
<span class="lineNum">     828 </span><span class="lineCov">        443 : static void GCTM (lua_State *L) {</span>
<span class="lineNum">     829 </span><span class="lineCov">        443 :   global_State *g = G(L);</span>
<span class="lineNum">     830 </span>            :   const TValue *tm;
<span class="lineNum">     831 </span>            :   TValue v;
<span class="lineNum">     832 </span>            :   lua_assert(!g-&gt;gcemergency);
<span class="lineNum">     833 </span><span class="lineCov">        443 :   setgcovalue(L, &amp;v, udata2finalize(g));</span>
<span class="lineNum">     834 </span><span class="lineCov">        443 :   tm = luaT_gettmbyobj(L, &amp;v, TM_GC);</span>
<span class="lineNum">     835 </span><span class="lineCov">        443 :   if (!notm(tm)) {  /* is there a finalizer? */</span>
<span class="lineNum">     836 </span>            :     int status;
<span class="lineNum">     837 </span><span class="lineCov">        443 :     lu_byte oldah = L-&gt;allowhook;</span>
<span class="lineNum">     838 </span><span class="lineCov">        443 :     int running  = g-&gt;gcrunning;</span>
<span class="lineNum">     839 </span><span class="lineCov">        443 :     L-&gt;allowhook = 0;  /* stop debug hooks during GC metamethod */</span>
<span class="lineNum">     840 </span><span class="lineCov">        443 :     g-&gt;gcrunning = 0;  /* avoid GC steps */</span>
<span class="lineNum">     841 </span><span class="lineCov">        443 :     setobj2s(L, L-&gt;top, tm);  /* push finalizer... */</span>
<span class="lineNum">     842 </span><span class="lineCov">        443 :     setobj2s(L, L-&gt;top + 1, &amp;v);  /* ... and its argument */</span>
<span class="lineNum">     843 </span><span class="lineCov">        443 :     L-&gt;top += 2;  /* and (next line) call the finalizer */</span>
<span class="lineNum">     844 </span><span class="lineCov">        443 :     L-&gt;ci-&gt;callstatus |= CIST_FIN;  /* will run a finalizer */</span>
<span class="lineNum">     845 </span><span class="lineCov">        443 :     status = luaD_pcall(L, dothecall, NULL, savestack(L, L-&gt;top - 2), 0);</span>
<span class="lineNum">     846 </span><span class="lineCov">        443 :     L-&gt;ci-&gt;callstatus &amp;= ~CIST_FIN;  /* not running a finalizer anymore */</span>
<span class="lineNum">     847 </span><span class="lineCov">        443 :     L-&gt;allowhook = oldah;  /* restore hooks */</span>
<span class="lineNum">     848 </span><span class="lineCov">        443 :     g-&gt;gcrunning = running;  /* restore state */</span>
<span class="lineNum">     849 </span><span class="lineCov">        443 :     if (status != LUA_OK) {  /* error while running __gc? */</span>
<span class="lineNum">     850 </span><span class="lineNoCov">          0 :       const char *msg = (ttisstring(s2v(L-&gt;top - 1)))</span>
<span class="lineNum">     851 </span><span class="lineNoCov">          0 :                         ? svalue(s2v(L-&gt;top - 1))</span>
<span class="lineNum">     852 </span><span class="lineNoCov">          0 :                         : &quot;error object is not a string&quot;;</span>
<span class="lineNum">     853 </span><span class="lineNoCov">          0 :       luaE_warning(L, &quot;error in __gc metamethod (&quot;, 1);</span>
<span class="lineNum">     854 </span><span class="lineNoCov">          0 :       luaE_warning(L, msg, 1);</span>
<span class="lineNum">     855 </span><span class="lineNoCov">          0 :       luaE_warning(L, &quot;)&quot;, 0);</span>
<span class="lineNum">     856 </span>            :     }
<span class="lineNum">     857 </span>            :   }
<span class="lineNum">     858 </span><span class="lineCov">        443 : }</span>
<span class="lineNum">     859 </span>            : 
<span class="lineNum">     860 </span>            : 
<span class="lineNum">     861 </span>            : /*
<span class="lineNum">     862 </span>            : ** Call a few finalizers
<span class="lineNum">     863 </span>            : */
<span class="lineNum">     864 </span><span class="lineNoCov">          0 : static int runafewfinalizers (lua_State *L, int n) {</span>
<span class="lineNum">     865 </span><span class="lineNoCov">          0 :   global_State *g = G(L);</span>
<span class="lineNum">     866 </span>            :   int i;
<span class="lineNum">     867 </span><span class="lineNoCov">          0 :   for (i = 0; i &lt; n &amp;&amp; g-&gt;tobefnz; i++)</span>
<span class="lineNum">     868 </span><span class="lineNoCov">          0 :     GCTM(L);  /* call one finalizer */</span>
<span class="lineNum">     869 </span><span class="lineNoCov">          0 :   return i;</span>
<span class="lineNum">     870 </span>            : }
<span class="lineNum">     871 </span>            : 
<span class="lineNum">     872 </span>            : 
<span class="lineNum">     873 </span>            : /*
<span class="lineNum">     874 </span>            : ** call all pending finalizers
<span class="lineNum">     875 </span>            : */
<span class="lineNum">     876 </span><span class="lineCov">        418 : static void callallpendingfinalizers (lua_State *L) {</span>
<span class="lineNum">     877 </span><span class="lineCov">        418 :   global_State *g = G(L);</span>
<span class="lineNum">     878 </span><span class="lineCov">        861 :   while (g-&gt;tobefnz)</span>
<span class="lineNum">     879 </span><span class="lineCov">        443 :     GCTM(L);</span>
<span class="lineNum">     880 </span><span class="lineCov">        418 : }</span>
<span class="lineNum">     881 </span>            : 
<span class="lineNum">     882 </span>            : 
<span class="lineNum">     883 </span>            : /*
<span class="lineNum">     884 </span>            : ** find last 'next' field in list 'p' list (to add elements in its end)
<span class="lineNum">     885 </span>            : */
<span class="lineNum">     886 </span><span class="lineCov">        712 : static GCObject **findlast (GCObject **p) {</span>
<span class="lineNum">     887 </span><span class="lineCov">        712 :   while (*p != NULL)</span>
<span class="lineNum">     888 </span><span class="lineNoCov">          0 :     p = &amp;(*p)-&gt;next;</span>
<span class="lineNum">     889 </span><span class="lineCov">        712 :   return p;</span>
<span class="lineNum">     890 </span>            : }
<span class="lineNum">     891 </span>            : 
<span class="lineNum">     892 </span>            : 
<span class="lineNum">     893 </span>            : /*
<span class="lineNum">     894 </span>            : ** Move all unreachable objects (or 'all' objects) that need
<span class="lineNum">     895 </span>            : ** finalization from list 'finobj' to list 'tobefnz' (to be finalized).
<span class="lineNum">     896 </span>            : ** (Note that objects after 'finobjold' cannot be white, so they
<span class="lineNum">     897 </span>            : ** don't need to be traversed. In incremental mode, 'finobjold' is NULL,
<span class="lineNum">     898 </span>            : ** so the whole list is traversed.)
<span class="lineNum">     899 </span>            : */
<span class="lineNum">     900 </span><span class="lineCov">        712 : static void separatetobefnz (global_State *g, int all) {</span>
<span class="lineNum">     901 </span>            :   GCObject *curr;
<span class="lineNum">     902 </span><span class="lineCov">        712 :   GCObject **p = &amp;g-&gt;finobj;</span>
<span class="lineNum">     903 </span><span class="lineCov">        712 :   GCObject **lastnext = findlast(&amp;g-&gt;tobefnz);</span>
<span class="lineNum">     904 </span><span class="lineCov">       2209 :   while ((curr = *p) != g-&gt;finobjold) {  /* traverse all finalizable objects */</span>
<span class="lineNum">     905 </span>            :     lua_assert(tofinalize(curr));
<span class="lineNum">     906 </span><span class="lineCov">       1497 :     if (!(iswhite(curr) || all))  /* not being collected? */</span>
<span class="lineNum">     907 </span><span class="lineCov">       1054 :       p = &amp;curr-&gt;next;  /* don't bother with it */</span>
<span class="lineNum">     908 </span>            :     else {
<span class="lineNum">     909 </span><span class="lineCov">        443 :       if (curr == g-&gt;finobjsur)  /* removing 'finobjsur'? */</span>
<span class="lineNum">     910 </span><span class="lineNoCov">          0 :         g-&gt;finobjsur = curr-&gt;next;  /* correct it */</span>
<span class="lineNum">     911 </span><span class="lineCov">        443 :       *p = curr-&gt;next;  /* remove 'curr' from 'finobj' list */</span>
<span class="lineNum">     912 </span><span class="lineCov">        443 :       curr-&gt;next = *lastnext;  /* link at the end of 'tobefnz' list */</span>
<span class="lineNum">     913 </span><span class="lineCov">        443 :       *lastnext = curr;</span>
<span class="lineNum">     914 </span><span class="lineCov">        443 :       lastnext = &amp;curr-&gt;next;</span>
<span class="lineNum">     915 </span>            :     }
<span class="lineNum">     916 </span>            :   }
<span class="lineNum">     917 </span><span class="lineCov">        712 : }</span>
<span class="lineNum">     918 </span>            : 
<span class="lineNum">     919 </span>            : 
<span class="lineNum">     920 </span>            : /*
<span class="lineNum">     921 </span>            : ** if object 'o' has a finalizer, remove it from 'allgc' list (must
<span class="lineNum">     922 </span>            : ** search the list to find it) and link it in 'finobj' list.
<span class="lineNum">     923 </span>            : */
<span class="lineNum">     924 </span><span class="lineCov">        575 : void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {</span>
<span class="lineNum">     925 </span><span class="lineCov">        575 :   global_State *g = G(L);</span>
<span class="lineNum">     926 </span><span class="lineCov">        575 :   if (tofinalize(o) ||                 /* obj. is already marked... */</span>
<span class="lineNum">     927 </span><span class="lineCov">        575 :       gfasttm(g, mt, TM_GC) == NULL)   /* or has no finalizer? */</span>
<span class="lineNum">     928 </span><span class="lineCov">         96 :     return;  /* nothing to be done */</span>
<span class="lineNum">     929 </span>            :   else {  /* move 'o' to 'finobj' list */
<span class="lineNum">     930 </span>            :     GCObject **p;
<span class="lineNum">     931 </span><span class="lineCov">        479 :     if (issweepphase(g)) {</span>
<span class="lineNum">     932 </span><span class="lineNoCov">          0 :       makewhite(g, o);  /* &quot;sweep&quot; object 'o' */</span>
<span class="lineNum">     933 </span><span class="lineNoCov">          0 :       if (g-&gt;sweepgc == &amp;o-&gt;next)  /* should not remove 'sweepgc' object */</span>
<span class="lineNum">     934 </span><span class="lineNoCov">          0 :         g-&gt;sweepgc = sweeptolive(L, g-&gt;sweepgc);  /* change 'sweepgc' */</span>
<span class="lineNum">     935 </span>            :     }
<span class="lineNum">     936 </span>            :     else {  /* correct pointers into 'allgc' list, if needed */
<span class="lineNum">     937 </span><span class="lineCov">        479 :       if (o == g-&gt;survival)</span>
<span class="lineNum">     938 </span><span class="lineNoCov">          0 :         g-&gt;survival = o-&gt;next;</span>
<span class="lineNum">     939 </span><span class="lineCov">        479 :       if (o == g-&gt;old)</span>
<span class="lineNum">     940 </span><span class="lineNoCov">          0 :         g-&gt;old = o-&gt;next;</span>
<span class="lineNum">     941 </span><span class="lineCov">        479 :       if (o == g-&gt;reallyold)</span>
<span class="lineNum">     942 </span><span class="lineNoCov">          0 :         g-&gt;reallyold = o-&gt;next;</span>
<span class="lineNum">     943 </span>            :     }
<span class="lineNum">     944 </span>            :     /* search for pointer pointing to 'o' */
<span class="lineNum">     945 </span><span class="lineCov">        570 :     for (p = &amp;g-&gt;allgc; *p != o; p = &amp;(*p)-&gt;next) { /* empty */ }</span>
<span class="lineNum">     946 </span><span class="lineCov">        479 :     *p = o-&gt;next;  /* remove 'o' from 'allgc' list */</span>
<span class="lineNum">     947 </span><span class="lineCov">        479 :     o-&gt;next = g-&gt;finobj;  /* link it in 'finobj' list */</span>
<span class="lineNum">     948 </span><span class="lineCov">        479 :     g-&gt;finobj = o;</span>
<span class="lineNum">     949 </span><span class="lineCov">        479 :     l_setbit(o-&gt;marked, FINALIZEDBIT);  /* mark it as such */</span>
<span class="lineNum">     950 </span>            :   }
<span class="lineNum">     951 </span>            : }
<span class="lineNum">     952 </span>            : 
<span class="lineNum">     953 </span>            : /* }====================================================== */
<span class="lineNum">     954 </span>            : 
<span class="lineNum">     955 </span>            : 
<span class="lineNum">     956 </span>            : /*
<span class="lineNum">     957 </span>            : ** {======================================================
<span class="lineNum">     958 </span>            : ** Generational Collector
<span class="lineNum">     959 </span>            : ** =======================================================
<span class="lineNum">     960 </span>            : */
<span class="lineNum">     961 </span>            : 
<span class="lineNum">     962 </span>            : static void setpause (global_State *g);
<span class="lineNum">     963 </span>            : 
<span class="lineNum">     964 </span>            : 
<span class="lineNum">     965 </span>            : /* mask to erase all color bits, not changing gen-related stuff */
<span class="lineNum">     966 </span>            : #define maskgencolors   (~(bitmask(BLACKBIT) | WHITEBITS))
<span class="lineNum">     967 </span>            : 
<span class="lineNum">     968 </span>            : 
<span class="lineNum">     969 </span>            : /*
<span class="lineNum">     970 </span>            : ** Sweep a list of objects, deleting dead ones and turning
<span class="lineNum">     971 </span>            : ** the non dead to old (without changing their colors).
<span class="lineNum">     972 </span>            : */
<span class="lineNum">     973 </span><span class="lineCov">        426 : static void sweep2old (lua_State *L, GCObject **p) {</span>
<span class="lineNum">     974 </span>            :   GCObject *curr;
<span class="lineNum">     975 </span><span class="lineCov">      41027 :   while ((curr = *p) != NULL) {</span>
<span class="lineNum">     976 </span><span class="lineCov">      40601 :     if (iswhite(curr)) {  /* is 'curr' dead? */</span>
<span class="lineNum">     977 </span>            :       lua_assert(isdead(G(L), curr));
<span class="lineNum">     978 </span><span class="lineCov">       3189 :       *p = curr-&gt;next;  /* remove 'curr' from list */</span>
<span class="lineNum">     979 </span><span class="lineCov">       3189 :       freeobj(L, curr);  /* erase 'curr' */</span>
<span class="lineNum">     980 </span>            :     }
<span class="lineNum">     981 </span>            :     else {  /* all surviving objects become old */
<span class="lineNum">     982 </span><span class="lineCov">      37412 :       setage(curr, G_OLD);</span>
<span class="lineNum">     983 </span><span class="lineCov">      37412 :       p = &amp;curr-&gt;next;  /* go to next element */</span>
<span class="lineNum">     984 </span>            :     }
<span class="lineNum">     985 </span>            :   }
<span class="lineNum">     986 </span><span class="lineCov">        426 : }</span>
<span class="lineNum">     987 </span>            : 
<span class="lineNum">     988 </span>            : 
<span class="lineNum">     989 </span>            : /*
<span class="lineNum">     990 </span>            : ** Sweep for generational mode. Delete dead objects. (Because the
<span class="lineNum">     991 </span>            : ** collection is not incremental, there are no &quot;new white&quot; objects
<span class="lineNum">     992 </span>            : ** during the sweep. So, any white object must be dead.) For
<span class="lineNum">     993 </span>            : ** non-dead objects, advance their ages and clear the color of
<span class="lineNum">     994 </span>            : ** new objects. (Old objects keep their colors.)
<span class="lineNum">     995 </span>            : */
<span class="lineNum">     996 </span><span class="lineCov">        910 : static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p,</span>
<span class="lineNum">     997 </span>            :                             GCObject *limit) {
<span class="lineNum">     998 </span>            :   static lu_byte nextage[] = {
<span class="lineNum">     999 </span>            :     G_SURVIVAL,  /* from G_NEW */
<span class="lineNum">    1000 </span>            :     G_OLD1,      /* from G_SURVIVAL */
<span class="lineNum">    1001 </span>            :     G_OLD1,      /* from G_OLD0 */
<span class="lineNum">    1002 </span>            :     G_OLD,       /* from G_OLD1 */
<span class="lineNum">    1003 </span>            :     G_OLD,       /* from G_OLD (do not change) */
<span class="lineNum">    1004 </span>            :     G_TOUCHED1,  /* from G_TOUCHED1 (do not change) */
<span class="lineNum">    1005 </span>            :     G_TOUCHED2   /* from G_TOUCHED2 (do not change) */
<span class="lineNum">    1006 </span>            :   };
<span class="lineNum">    1007 </span><span class="lineCov">        910 :   int white = luaC_white(g);</span>
<span class="lineNum">    1008 </span>            :   GCObject *curr;
<span class="lineNum">    1009 </span><span class="lineCov">      21682 :   while ((curr = *p) != limit) {</span>
<span class="lineNum">    1010 </span><span class="lineCov">      20772 :     if (iswhite(curr)) {  /* is 'curr' dead? */</span>
<span class="lineNum">    1011 </span>            :       lua_assert(!isold(curr) &amp;&amp; isdead(g, curr));
<span class="lineNum">    1012 </span><span class="lineCov">      17087 :       *p = curr-&gt;next;  /* remove 'curr' from list */</span>
<span class="lineNum">    1013 </span><span class="lineCov">      17087 :       freeobj(L, curr);  /* erase 'curr' */</span>
<span class="lineNum">    1014 </span>            :     }
<span class="lineNum">    1015 </span>            :     else {  /* correct mark and age */
<span class="lineNum">    1016 </span><span class="lineCov">       3685 :       if (getage(curr) == G_NEW)</span>
<span class="lineNum">    1017 </span><span class="lineCov">       3648 :         curr-&gt;marked = cast_byte((curr-&gt;marked &amp; maskgencolors) | white);</span>
<span class="lineNum">    1018 </span><span class="lineCov">       3685 :       setage(curr, nextage[getage(curr)]);</span>
<span class="lineNum">    1019 </span><span class="lineCov">       3685 :       p = &amp;curr-&gt;next;  /* go to next element */</span>
<span class="lineNum">    1020 </span>            :     }
<span class="lineNum">    1021 </span>            :   }
<span class="lineNum">    1022 </span><span class="lineCov">        910 :   return p;</span>
<span class="lineNum">    1023 </span>            : }
<span class="lineNum">    1024 </span>            : 
<span class="lineNum">    1025 </span>            : 
<span class="lineNum">    1026 </span>            : /*
<span class="lineNum">    1027 </span>            : ** Traverse a list making all its elements white and clearing their
<span class="lineNum">    1028 </span>            : ** age.
<span class="lineNum">    1029 </span>            : */
<span class="lineNum">    1030 </span><span class="lineCov">        399 : static void whitelist (global_State *g, GCObject *p) {</span>
<span class="lineNum">    1031 </span><span class="lineCov">        399 :   int white = luaC_white(g);</span>
<span class="lineNum">    1032 </span><span class="lineCov">      53786 :   for (; p != NULL; p = p-&gt;next)</span>
<span class="lineNum">    1033 </span><span class="lineCov">      53387 :     p-&gt;marked = cast_byte((p-&gt;marked &amp; maskcolors) | white);</span>
<span class="lineNum">    1034 </span><span class="lineCov">        399 : }</span>
<span class="lineNum">    1035 </span>            : 
<span class="lineNum">    1036 </span>            : 
<span class="lineNum">    1037 </span>            : /*
<span class="lineNum">    1038 </span>            : ** Correct a list of gray objects.
<span class="lineNum">    1039 </span>            : ** Because this correction is done after sweeping, young objects might
<span class="lineNum">    1040 </span>            : ** be turned white and still be in the list. They are only removed.
<span class="lineNum">    1041 </span>            : ** For tables and userdata, advance 'touched1' to 'touched2'; 'touched2'
<span class="lineNum">    1042 </span>            : ** objects become regular old and are removed from the list.
<span class="lineNum">    1043 </span>            : ** For threads, just remove white ones from the list.
<span class="lineNum">    1044 </span>            : */
<span class="lineNum">    1045 </span><span class="lineCov">       1296 : static GCObject **correctgraylist (GCObject **p) {</span>
<span class="lineNum">    1046 </span>            :   GCObject *curr;
<span class="lineNum">    1047 </span><span class="lineCov">       1296 :   while ((curr = *p) != NULL) {</span>
<span class="lineNum">    1048 </span><span class="lineCov">        762 :     switch (curr-&gt;tt) {</span>
<span class="lineNum">    1049 </span><span class="lineCov">        251 :       case LUA_TTABLE: case LUA_TUSERDATA: {</span>
<span class="lineNum">    1050 </span><span class="lineCov">        251 :         GCObject **next = getgclist(curr);</span>
<span class="lineNum">    1051 </span><span class="lineCov">        251 :         if (getage(curr) == G_TOUCHED1) {  /* touched in this cycle? */</span>
<span class="lineNum">    1052 </span>            :           lua_assert(isgray(curr));
<span class="lineNum">    1053 </span><span class="lineCov">         61 :           gray2black(curr);  /* make it black, for next barrier */</span>
<span class="lineNum">    1054 </span><span class="lineCov">         61 :           changeage(curr, G_TOUCHED1, G_TOUCHED2);</span>
<span class="lineNum">    1055 </span><span class="lineCov">         61 :           p = next;  /* go to next element */</span>
<span class="lineNum">    1056 </span>            :         }
<span class="lineNum">    1057 </span>            :         else {  /* not touched in this cycle */
<span class="lineNum">    1058 </span><span class="lineCov">        190 :           if (!iswhite(curr)) {  /* not white? */</span>
<span class="lineNum">    1059 </span>            :             lua_assert(isold(curr));
<span class="lineNum">    1060 </span><span class="lineNoCov">          0 :             if (getage(curr) == G_TOUCHED2)  /* advance from G_TOUCHED2... */</span>
<span class="lineNum">    1061 </span><span class="lineNoCov">          0 :               changeage(curr, G_TOUCHED2, G_OLD);  /* ... to G_OLD */</span>
<span class="lineNum">    1062 </span><span class="lineNoCov">          0 :             gray2black(curr);  /* make it black */</span>
<span class="lineNum">    1063 </span>            :           }
<span class="lineNum">    1064 </span>            :           /* else, object is white: just remove it from this list */
<span class="lineNum">    1065 </span><span class="lineCov">        190 :           *p = *next;  /* remove 'curr' from gray list */</span>
<span class="lineNum">    1066 </span>            :         }
<span class="lineNum">    1067 </span><span class="lineCov">        251 :         break;</span>
<span class="lineNum">    1068 </span>            :       }
<span class="lineNum">    1069 </span><span class="lineCov">        511 :       case LUA_TTHREAD: {</span>
<span class="lineNum">    1070 </span><span class="lineCov">        511 :         lua_State *th = gco2th(curr);</span>
<span class="lineNum">    1071 </span>            :         lua_assert(!isblack(th));
<span class="lineNum">    1072 </span><span class="lineCov">        511 :         if (iswhite(th))  /* new object? */</span>
<span class="lineNum">    1073 </span><span class="lineCov">          1 :           *p = th-&gt;gclist;  /* remove from gray list */</span>
<span class="lineNum">    1074 </span>            :         else  /* old threads remain gray */
<span class="lineNum">    1075 </span><span class="lineCov">        510 :           p = &amp;th-&gt;gclist;  /* go to next element */</span>
<span class="lineNum">    1076 </span><span class="lineCov">        511 :         break;</span>
<span class="lineNum">    1077 </span>            :       }
<span class="lineNum">    1078 </span><span class="lineCov">       2058 :       default: lua_assert(0);  /* nothing more could be gray here */</span>
<span class="lineNum">    1079 </span>            :     }
<span class="lineNum">    1080 </span>            :   }
<span class="lineNum">    1081 </span><span class="lineCov">       1296 :   return p;</span>
<span class="lineNum">    1082 </span>            : }
<span class="lineNum">    1083 </span>            : 
<span class="lineNum">    1084 </span>            : 
<span class="lineNum">    1085 </span>            : /*
<span class="lineNum">    1086 </span>            : ** Correct all gray lists, coalescing them into 'grayagain'.
<span class="lineNum">    1087 </span>            : */
<span class="lineNum">    1088 </span><span class="lineCov">        324 : static void correctgraylists (global_State *g) {</span>
<span class="lineNum">    1089 </span><span class="lineCov">        324 :   GCObject **list = correctgraylist(&amp;g-&gt;grayagain);</span>
<span class="lineNum">    1090 </span><span class="lineCov">        324 :   *list = g-&gt;weak; g-&gt;weak = NULL;</span>
<span class="lineNum">    1091 </span><span class="lineCov">        324 :   list = correctgraylist(list);</span>
<span class="lineNum">    1092 </span><span class="lineCov">        324 :   *list = g-&gt;allweak; g-&gt;allweak = NULL;</span>
<span class="lineNum">    1093 </span><span class="lineCov">        324 :   list = correctgraylist(list);</span>
<span class="lineNum">    1094 </span><span class="lineCov">        324 :   *list = g-&gt;ephemeron; g-&gt;ephemeron = NULL;</span>
<span class="lineNum">    1095 </span><span class="lineCov">        324 :   correctgraylist(list);</span>
<span class="lineNum">    1096 </span><span class="lineCov">        324 : }</span>
<span class="lineNum">    1097 </span>            : 
<span class="lineNum">    1098 </span>            : 
<span class="lineNum">    1099 </span>            : /*
<span class="lineNum">    1100 </span>            : ** Mark 'OLD1' objects when starting a new young collection.
<span class="lineNum">    1101 </span>            : ** Gray objects are already in some gray list, and so will be visited
<span class="lineNum">    1102 </span>            : ** in the atomic step.
<span class="lineNum">    1103 </span>            : */
<span class="lineNum">    1104 </span><span class="lineCov">        364 : static void markold (global_State *g, GCObject *from, GCObject *to) {</span>
<span class="lineNum">    1105 </span>            :   GCObject *p;
<span class="lineNum">    1106 </span><span class="lineCov">        849 :   for (p = from; p != to; p = p-&gt;next) {</span>
<span class="lineNum">    1107 </span><span class="lineCov">        485 :     if (getage(p) == G_OLD1) {</span>
<span class="lineNum">    1108 </span>            :       lua_assert(!iswhite(p));
<span class="lineNum">    1109 </span><span class="lineCov">         18 :       if (isblack(p)) {</span>
<span class="lineNum">    1110 </span><span class="lineCov">         17 :         black2gray(p);  /* should be '2white', but gray works too */</span>
<span class="lineNum">    1111 </span><span class="lineCov">         17 :         reallymarkobject(g, p);</span>
<span class="lineNum">    1112 </span>            :       }
<span class="lineNum">    1113 </span>            :     }
<span class="lineNum">    1114 </span>            :   }
<span class="lineNum">    1115 </span><span class="lineCov">        364 : }</span>
<span class="lineNum">    1116 </span>            : 
<span class="lineNum">    1117 </span>            : 
<span class="lineNum">    1118 </span>            : /*
<span class="lineNum">    1119 </span>            : ** Finish a young-generation collection.
<span class="lineNum">    1120 </span>            : */
<span class="lineNum">    1121 </span><span class="lineCov">        324 : static void finishgencycle (lua_State *L, global_State *g) {</span>
<span class="lineNum">    1122 </span><span class="lineCov">        324 :   correctgraylists(g);</span>
<span class="lineNum">    1123 </span><span class="lineCov">        324 :   checkSizes(L, g);</span>
<span class="lineNum">    1124 </span><span class="lineCov">        324 :   g-&gt;gcstate = GCSpropagate;  /* skip restart */</span>
<span class="lineNum">    1125 </span><span class="lineCov">        324 :   if (!g-&gt;gcemergency)</span>
<span class="lineNum">    1126 </span><span class="lineCov">        324 :     callallpendingfinalizers(L);</span>
<span class="lineNum">    1127 </span><span class="lineCov">        324 : }</span>
<span class="lineNum">    1128 </span>            : 
<span class="lineNum">    1129 </span>            : 
<span class="lineNum">    1130 </span>            : /*
<span class="lineNum">    1131 </span>            : ** Does a young collection. First, mark 'OLD1' objects.  (Only survival
<span class="lineNum">    1132 </span>            : ** and &quot;recent old&quot; lists can contain 'OLD1' objects. New lists cannot
<span class="lineNum">    1133 </span>            : ** contain 'OLD1' objects, at most 'OLD0' objects that were already
<span class="lineNum">    1134 </span>            : ** visited when marked old.) Then does the atomic step. Then,
<span class="lineNum">    1135 </span>            : ** sweep all lists and advance pointers. Finally, finish the collection.
<span class="lineNum">    1136 </span>            : */
<span class="lineNum">    1137 </span><span class="lineCov">        182 : static void youngcollection (lua_State *L, global_State *g) {</span>
<span class="lineNum">    1138 </span>            :   GCObject **psurvival;  /* to point to first non-dead survival object */
<span class="lineNum">    1139 </span>            :   lua_assert(g-&gt;gcstate == GCSpropagate);
<span class="lineNum">    1140 </span><span class="lineCov">        182 :   markold(g, g-&gt;survival, g-&gt;reallyold);</span>
<span class="lineNum">    1141 </span><span class="lineCov">        182 :   markold(g, g-&gt;finobj, g-&gt;finobjrold);</span>
<span class="lineNum">    1142 </span><span class="lineCov">        182 :   atomic(L);</span>
<span class="lineNum">    1143 </span>            : 
<span class="lineNum">    1144 </span>            :   /* sweep nursery and get a pointer to its last live element */
<span class="lineNum">    1145 </span><span class="lineCov">        182 :   psurvival = sweepgen(L, g, &amp;g-&gt;allgc, g-&gt;survival);</span>
<span class="lineNum">    1146 </span>            :   /* sweep 'survival' and 'old' */
<span class="lineNum">    1147 </span><span class="lineCov">        182 :   sweepgen(L, g, psurvival, g-&gt;reallyold);</span>
<span class="lineNum">    1148 </span><span class="lineCov">        182 :   g-&gt;reallyold = g-&gt;old;</span>
<span class="lineNum">    1149 </span><span class="lineCov">        182 :   g-&gt;old = *psurvival;  /* 'survival' survivals are old now */</span>
<span class="lineNum">    1150 </span><span class="lineCov">        182 :   g-&gt;survival = g-&gt;allgc;  /* all news are survivals */</span>
<span class="lineNum">    1151 </span>            : 
<span class="lineNum">    1152 </span>            :   /* repeat for 'finobj' lists */
<span class="lineNum">    1153 </span><span class="lineCov">        182 :   psurvival = sweepgen(L, g, &amp;g-&gt;finobj, g-&gt;finobjsur);</span>
<span class="lineNum">    1154 </span>            :   /* sweep 'survival' and 'old' */
<span class="lineNum">    1155 </span><span class="lineCov">        182 :   sweepgen(L, g, psurvival, g-&gt;finobjrold);</span>
<span class="lineNum">    1156 </span><span class="lineCov">        182 :   g-&gt;finobjrold = g-&gt;finobjold;</span>
<span class="lineNum">    1157 </span><span class="lineCov">        182 :   g-&gt;finobjold = *psurvival;  /* 'survival' survivals are old now */</span>
<span class="lineNum">    1158 </span><span class="lineCov">        182 :   g-&gt;finobjsur = g-&gt;finobj;  /* all news are survivals */</span>
<span class="lineNum">    1159 </span>            : 
<span class="lineNum">    1160 </span><span class="lineCov">        182 :   sweepgen(L, g, &amp;g-&gt;tobefnz, NULL);</span>
<span class="lineNum">    1161 </span>            : 
<span class="lineNum">    1162 </span><span class="lineCov">        182 :   finishgencycle(L, g);</span>
<span class="lineNum">    1163 </span><span class="lineCov">        182 : }</span>
<span class="lineNum">    1164 </span>            : 
<span class="lineNum">    1165 </span>            : 
<span class="lineNum">    1166 </span><span class="lineCov">        142 : static void atomic2gen (lua_State *L, global_State *g) {</span>
<span class="lineNum">    1167 </span>            :   /* sweep all elements making them old */
<span class="lineNum">    1168 </span><span class="lineCov">        142 :   sweep2old(L, &amp;g-&gt;allgc);</span>
<span class="lineNum">    1169 </span>            :   /* everything alive now is old */
<span class="lineNum">    1170 </span><span class="lineCov">        142 :   g-&gt;reallyold = g-&gt;old = g-&gt;survival = g-&gt;allgc;</span>
<span class="lineNum">    1171 </span>            : 
<span class="lineNum">    1172 </span>            :   /* repeat for 'finobj' lists */
<span class="lineNum">    1173 </span><span class="lineCov">        142 :   sweep2old(L, &amp;g-&gt;finobj);</span>
<span class="lineNum">    1174 </span><span class="lineCov">        142 :   g-&gt;finobjrold = g-&gt;finobjold = g-&gt;finobjsur = g-&gt;finobj;</span>
<span class="lineNum">    1175 </span>            : 
<span class="lineNum">    1176 </span><span class="lineCov">        142 :   sweep2old(L, &amp;g-&gt;tobefnz);</span>
<span class="lineNum">    1177 </span>            : 
<span class="lineNum">    1178 </span><span class="lineCov">        142 :   g-&gt;gckind = KGC_GEN;</span>
<span class="lineNum">    1179 </span><span class="lineCov">        142 :   g-&gt;lastatomic = 0;</span>
<span class="lineNum">    1180 </span><span class="lineCov">        142 :   g-&gt;GCestimate = gettotalbytes(g);  /* base for memory control */</span>
<span class="lineNum">    1181 </span><span class="lineCov">        142 :   finishgencycle(L, g);</span>
<span class="lineNum">    1182 </span><span class="lineCov">        142 : }</span>
<span class="lineNum">    1183 </span>            : 
<span class="lineNum">    1184 </span>            : 
<span class="lineNum">    1185 </span>            : /*
<span class="lineNum">    1186 </span>            : ** Enter generational mode. Must go until the end of an atomic cycle
<span class="lineNum">    1187 </span>            : ** to ensure that all threads and weak tables are in the gray lists.
<span class="lineNum">    1188 </span>            : ** Then, turn all objects into old and finishes the collection.
<span class="lineNum">    1189 </span>            : */
<span class="lineNum">    1190 </span><span class="lineCov">        134 : static lu_mem entergen (lua_State *L, global_State *g) {</span>
<span class="lineNum">    1191 </span>            :   lu_mem numobjs;
<span class="lineNum">    1192 </span><span class="lineCov">        134 :   luaC_runtilstate(L, bitmask(GCSpause));  /* prepare to start a new cycle */</span>
<span class="lineNum">    1193 </span><span class="lineCov">        134 :   luaC_runtilstate(L, bitmask(GCSpropagate));  /* start new cycle */</span>
<span class="lineNum">    1194 </span><span class="lineCov">        134 :   numobjs = atomic(L);  /* propagates all and then do the atomic stuff */</span>
<span class="lineNum">    1195 </span><span class="lineCov">        134 :   atomic2gen(L, g);</span>
<span class="lineNum">    1196 </span><span class="lineCov">        134 :   return numobjs;</span>
<span class="lineNum">    1197 </span>            : }
<span class="lineNum">    1198 </span>            : 
<span class="lineNum">    1199 </span>            : 
<span class="lineNum">    1200 </span>            : /*
<span class="lineNum">    1201 </span>            : ** Enter incremental mode. Turn all objects white, make all
<span class="lineNum">    1202 </span>            : ** intermediate lists point to NULL (to avoid invalid pointers),
<span class="lineNum">    1203 </span>            : ** and go to the pause state.
<span class="lineNum">    1204 </span>            : */
<span class="lineNum">    1205 </span><span class="lineCov">        133 : static void enterinc (global_State *g) {</span>
<span class="lineNum">    1206 </span><span class="lineCov">        133 :   whitelist(g, g-&gt;allgc);</span>
<span class="lineNum">    1207 </span><span class="lineCov">        133 :   g-&gt;reallyold = g-&gt;old = g-&gt;survival = NULL;</span>
<span class="lineNum">    1208 </span><span class="lineCov">        133 :   whitelist(g, g-&gt;finobj);</span>
<span class="lineNum">    1209 </span><span class="lineCov">        133 :   whitelist(g, g-&gt;tobefnz);</span>
<span class="lineNum">    1210 </span><span class="lineCov">        133 :   g-&gt;finobjrold = g-&gt;finobjold = g-&gt;finobjsur = NULL;</span>
<span class="lineNum">    1211 </span><span class="lineCov">        133 :   g-&gt;gcstate = GCSpause;</span>
<span class="lineNum">    1212 </span><span class="lineCov">        133 :   g-&gt;gckind = KGC_INC;</span>
<span class="lineNum">    1213 </span><span class="lineCov">        133 :   g-&gt;lastatomic = 0;</span>
<span class="lineNum">    1214 </span><span class="lineCov">        133 : }</span>
<span class="lineNum">    1215 </span>            : 
<span class="lineNum">    1216 </span>            : 
<span class="lineNum">    1217 </span>            : /*
<span class="lineNum">    1218 </span>            : ** Change collector mode to 'newmode'.
<span class="lineNum">    1219 </span>            : */
<span class="lineNum">    1220 </span><span class="lineCov">        187 : void luaC_changemode (lua_State *L, int newmode) {</span>
<span class="lineNum">    1221 </span><span class="lineCov">        187 :   global_State *g = G(L);</span>
<span class="lineNum">    1222 </span><span class="lineCov">        187 :   if (newmode != g-&gt;gckind) {</span>
<span class="lineNum">    1223 </span><span class="lineCov">        168 :     if (newmode == KGC_GEN)  /* entering generational mode? */</span>
<span class="lineNum">    1224 </span><span class="lineCov">         91 :       entergen(L, g);</span>
<span class="lineNum">    1225 </span>            :     else
<span class="lineNum">    1226 </span><span class="lineCov">         77 :       enterinc(g);  /* entering incremental mode */</span>
<span class="lineNum">    1227 </span>            :   }
<span class="lineNum">    1228 </span><span class="lineCov">        187 :   g-&gt;lastatomic = 0;</span>
<span class="lineNum">    1229 </span><span class="lineCov">        187 : }</span>
<span class="lineNum">    1230 </span>            : 
<span class="lineNum">    1231 </span>            : 
<span class="lineNum">    1232 </span>            : /*
<span class="lineNum">    1233 </span>            : ** Does a full collection in generational mode.
<span class="lineNum">    1234 </span>            : */
<span class="lineNum">    1235 </span><span class="lineCov">         43 : static lu_mem fullgen (lua_State *L, global_State *g) {</span>
<span class="lineNum">    1236 </span><span class="lineCov">         43 :   enterinc(g);</span>
<span class="lineNum">    1237 </span><span class="lineCov">         43 :   return entergen(L, g);</span>
<span class="lineNum">    1238 </span>            : }
<span class="lineNum">    1239 </span>            : 
<span class="lineNum">    1240 </span>            : 
<span class="lineNum">    1241 </span>            : /*
<span class="lineNum">    1242 </span>            : ** Set debt for the next minor collection, which will happen when
<span class="lineNum">    1243 </span>            : ** memory grows 'genminormul'%.
<span class="lineNum">    1244 </span>            : */
<span class="lineNum">    1245 </span><span class="lineCov">        190 : static void setminordebt (global_State *g) {</span>
<span class="lineNum">    1246 </span><span class="lineCov">        190 :   luaE_setdebt(g, -(cast(l_mem, (gettotalbytes(g) / 100)) * g-&gt;genminormul));</span>
<span class="lineNum">    1247 </span><span class="lineCov">        190 : }</span>
<span class="lineNum">    1248 </span>            : 
<span class="lineNum">    1249 </span>            : 
<span class="lineNum">    1250 </span>            : /*
<span class="lineNum">    1251 </span>            : ** Does a major collection after last collection was a &quot;bad collection&quot;.
<span class="lineNum">    1252 </span>            : **
<span class="lineNum">    1253 </span>            : ** When the program is building a big struture, it allocates lots of
<span class="lineNum">    1254 </span>            : ** memory but generates very little garbage. In those scenarios,
<span class="lineNum">    1255 </span>            : ** the generational mode just wastes time doing small collections, and
<span class="lineNum">    1256 </span>            : ** major collections are frequently what we call a &quot;bad collection&quot;, a
<span class="lineNum">    1257 </span>            : ** collection that frees too few objects. To avoid the cost of switching
<span class="lineNum">    1258 </span>            : ** between generational mode and the incremental mode needed for full
<span class="lineNum">    1259 </span>            : ** (major) collections, the collector tries to stay in incremental mode
<span class="lineNum">    1260 </span>            : ** after a bad collection, and to switch back to generational mode only
<span class="lineNum">    1261 </span>            : ** after a &quot;good&quot; collection (one that traverses less than 9/8 objects
<span class="lineNum">    1262 </span>            : ** of the previous one).
<span class="lineNum">    1263 </span>            : ** The collector must choose whether to stay in incremental mode or to
<span class="lineNum">    1264 </span>            : ** switch back to generational mode before sweeping. At this point, it
<span class="lineNum">    1265 </span>            : ** does not know the real memory in use, so it cannot use memory to
<span class="lineNum">    1266 </span>            : ** decide whether to return to generational mode. Instead, it uses the
<span class="lineNum">    1267 </span>            : ** number of objects traversed (returned by 'atomic') as a proxy. The
<span class="lineNum">    1268 </span>            : ** field 'g-&gt;lastatomic' keeps this count from the last collection.
<span class="lineNum">    1269 </span>            : ** ('g-&gt;lastatomic != 0' also means that the last collection was bad.)
<span class="lineNum">    1270 </span>            : */
<span class="lineNum">    1271 </span><span class="lineCov">         16 : static void stepgenfull (lua_State *L, global_State *g) {</span>
<span class="lineNum">    1272 </span>            :   lu_mem newatomic;  /* count of traversed objects */
<span class="lineNum">    1273 </span><span class="lineCov">         16 :   lu_mem lastatomic = g-&gt;lastatomic;  /* count from last collection */</span>
<span class="lineNum">    1274 </span><span class="lineCov">         16 :   if (g-&gt;gckind == KGC_GEN)  /* still in generational mode? */</span>
<span class="lineNum">    1275 </span><span class="lineCov">         13 :     enterinc(g);  /* enter incremental mode */</span>
<span class="lineNum">    1276 </span><span class="lineCov">         16 :   luaC_runtilstate(L, bitmask(GCSpropagate));  /* start new cycle */</span>
<span class="lineNum">    1277 </span><span class="lineCov">         16 :   newatomic = atomic(L);  /* mark everybody */</span>
<span class="lineNum">    1278 </span><span class="lineCov">         16 :   if (newatomic &lt; lastatomic + (lastatomic &gt;&gt; 3)) {  /* good collection? */</span>
<span class="lineNum">    1279 </span><span class="lineCov">          8 :     atomic2gen(L, g);  /* return to generational mode */</span>
<span class="lineNum">    1280 </span><span class="lineCov">          8 :     setminordebt(g);</span>
<span class="lineNum">    1281 </span>            :   }
<span class="lineNum">    1282 </span>            :   else {  /* another bad collection; stay in incremental mode */
<span class="lineNum">    1283 </span><span class="lineCov">          8 :     g-&gt;GCestimate = gettotalbytes(g);  /* first estimate */;</span>
<span class="lineNum">    1284 </span><span class="lineCov">          8 :     entersweep(L);</span>
<span class="lineNum">    1285 </span><span class="lineCov">          8 :     luaC_runtilstate(L, bitmask(GCSpause));  /* finish collection */</span>
<span class="lineNum">    1286 </span><span class="lineCov">          8 :     setpause(g);</span>
<span class="lineNum">    1287 </span><span class="lineCov">          8 :     g-&gt;lastatomic = newatomic;</span>
<span class="lineNum">    1288 </span>            :   }
<span class="lineNum">    1289 </span><span class="lineCov">         16 : }</span>
<span class="lineNum">    1290 </span>            : 
<span class="lineNum">    1291 </span>            : 
<span class="lineNum">    1292 </span>            : /*
<span class="lineNum">    1293 </span>            : ** Does a generational &quot;step&quot;.
<span class="lineNum">    1294 </span>            : ** Usually, this means doing a minor collection and setting the debt to
<span class="lineNum">    1295 </span>            : ** make another collection when memory grows 'genminormul'% larger.
<span class="lineNum">    1296 </span>            : **
<span class="lineNum">    1297 </span>            : ** However, there are exceptions.  If memory grows 'genmajormul'%
<span class="lineNum">    1298 </span>            : ** larger than it was at the end of the last major collection (kept
<span class="lineNum">    1299 </span>            : ** in 'g-&gt;GCestimate'), the function does a major collection. At the
<span class="lineNum">    1300 </span>            : ** end, it checks whether the major collection was able to free a
<span class="lineNum">    1301 </span>            : ** decent amount of memory (at least half the growth in memory since
<span class="lineNum">    1302 </span>            : ** previous major collection). If so, the collector keeps its state,
<span class="lineNum">    1303 </span>            : ** and the next collection will probably be minor again. Otherwise,
<span class="lineNum">    1304 </span>            : ** we have what we call a &quot;bad collection&quot;. In that case, set the field
<span class="lineNum">    1305 </span>            : ** 'g-&gt;lastatomic' to signal that fact, so that the next collection will
<span class="lineNum">    1306 </span>            : ** go to 'stepgenfull'.
<span class="lineNum">    1307 </span>            : **
<span class="lineNum">    1308 </span>            : ** 'GCdebt &lt;= 0' means an explicit call to GC step with &quot;size&quot; zero;
<span class="lineNum">    1309 </span>            : ** in that case, do a minor collection.
<span class="lineNum">    1310 </span>            : */
<span class="lineNum">    1311 </span><span class="lineCov">        239 : static void genstep (lua_State *L, global_State *g) {</span>
<span class="lineNum">    1312 </span><span class="lineCov">        239 :   if (g-&gt;lastatomic != 0)  /* last collection was a bad one? */</span>
<span class="lineNum">    1313 </span><span class="lineCov">         16 :     stepgenfull(L, g);  /* do a full step */</span>
<span class="lineNum">    1314 </span>            :   else {
<span class="lineNum">    1315 </span><span class="lineCov">        223 :     lu_mem majorbase = g-&gt;GCestimate;  /* memory after last major collection */</span>
<span class="lineNum">    1316 </span><span class="lineCov">        223 :     lu_mem majorinc = (majorbase / 100) * getgcparam(g-&gt;genmajormul);</span>
<span class="lineNum">    1317 </span><span class="lineCov">        264 :     if (g-&gt;GCdebt &gt; 0 &amp;&amp; gettotalbytes(g) &gt; majorbase + majorinc) {</span>
<span class="lineNum">    1318 </span><span class="lineCov">         41 :       lu_mem numobjs = fullgen(L, g);  /* do a major collection */</span>
<span class="lineNum">    1319 </span><span class="lineCov">         41 :       if (gettotalbytes(g) &lt; majorbase + (majorinc / 2)) {</span>
<span class="lineNum">    1320 </span>            :         /* collected at least half of memory growth since last major
<span class="lineNum">    1321 </span>            :            collection; keep doing minor collections */
<span class="lineNum">    1322 </span><span class="lineNoCov">          0 :         setminordebt(g);</span>
<span class="lineNum">    1323 </span>            :       }
<span class="lineNum">    1324 </span>            :       else {  /* bad collection */
<span class="lineNum">    1325 </span><span class="lineCov">         41 :         g-&gt;lastatomic = numobjs;  /* signal that last collection was bad */</span>
<span class="lineNum">    1326 </span><span class="lineCov">         41 :         setpause(g);  /* do a long wait for next (major) collection */</span>
<span class="lineNum">    1327 </span>            :       }
<span class="lineNum">    1328 </span>            :     }
<span class="lineNum">    1329 </span>            :     else {  /* regular case; do a minor collection */
<span class="lineNum">    1330 </span><span class="lineCov">        182 :       youngcollection(L, g);</span>
<span class="lineNum">    1331 </span><span class="lineCov">        182 :       setminordebt(g);</span>
<span class="lineNum">    1332 </span><span class="lineCov">        182 :       g-&gt;GCestimate = majorbase;  /* preserve base value */</span>
<span class="lineNum">    1333 </span>            :     }
<span class="lineNum">    1334 </span>            :   }
<span class="lineNum">    1335 </span>            :   lua_assert(isdecGCmodegen(g));
<span class="lineNum">    1336 </span><span class="lineCov">        239 : }</span>
<span class="lineNum">    1337 </span>            : 
<span class="lineNum">    1338 </span>            : /* }====================================================== */
<span class="lineNum">    1339 </span>            : 
<span class="lineNum">    1340 </span>            : 
<span class="lineNum">    1341 </span>            : /*
<span class="lineNum">    1342 </span>            : ** {======================================================
<span class="lineNum">    1343 </span>            : ** GC control
<span class="lineNum">    1344 </span>            : ** =======================================================
<span class="lineNum">    1345 </span>            : */
<span class="lineNum">    1346 </span>            : 
<span class="lineNum">    1347 </span>            : 
<span class="lineNum">    1348 </span>            : /*
<span class="lineNum">    1349 </span>            : ** Set the &quot;time&quot; to wait before starting a new GC cycle; cycle will
<span class="lineNum">    1350 </span>            : ** start when memory use hits the threshold of ('estimate' * pause /
<span class="lineNum">    1351 </span>            : ** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero,
<span class="lineNum">    1352 </span>            : ** because Lua cannot even start with less than PAUSEADJ bytes).
<span class="lineNum">    1353 </span>            : */
<span class="lineNum">    1354 </span><span class="lineCov">        335 : static void setpause (global_State *g) {</span>
<span class="lineNum">    1355 </span>            :   l_mem threshold, debt;
<span class="lineNum">    1356 </span><span class="lineCov">        335 :   int pause = getgcparam(g-&gt;gcpause);</span>
<span class="lineNum">    1357 </span><span class="lineCov">        335 :   l_mem estimate = g-&gt;GCestimate / PAUSEADJ;  /* adjust 'estimate' */</span>
<span class="lineNum">    1358 </span>            :   lua_assert(estimate &gt; 0);
<span class="lineNum">    1359 </span><span class="lineCov">        670 :   threshold = (pause &lt; MAX_LMEM / estimate)  /* overflow? */</span>
<span class="lineNum">    1360 </span><span class="lineCov">        335 :             ? estimate * pause  /* no overflow */</span>
<span class="lineNum">    1361 </span><span class="lineCov">        335 :             : MAX_LMEM;  /* overflow; truncate to maximum */</span>
<span class="lineNum">    1362 </span><span class="lineCov">        335 :   debt = gettotalbytes(g) - threshold;</span>
<span class="lineNum">    1363 </span><span class="lineCov">        335 :   if (debt &gt; 0) debt = 0;</span>
<span class="lineNum">    1364 </span><span class="lineCov">        335 :   luaE_setdebt(g, debt);</span>
<span class="lineNum">    1365 </span><span class="lineCov">        335 : }</span>
<span class="lineNum">    1366 </span>            : 
<span class="lineNum">    1367 </span>            : 
<span class="lineNum">    1368 </span>            : /*
<span class="lineNum">    1369 </span>            : ** Enter first sweep phase.
<span class="lineNum">    1370 </span>            : ** The call to 'sweeptolive' makes the pointer point to an object
<span class="lineNum">    1371 </span>            : ** inside the list (instead of to the header), so that the real sweep do
<span class="lineNum">    1372 </span>            : ** not need to skip objects created between &quot;now&quot; and the start of the
<span class="lineNum">    1373 </span>            : ** real sweep.
<span class="lineNum">    1374 </span>            : */
<span class="lineNum">    1375 </span><span class="lineCov">        294 : static void entersweep (lua_State *L) {</span>
<span class="lineNum">    1376 </span><span class="lineCov">        294 :   global_State *g = G(L);</span>
<span class="lineNum">    1377 </span><span class="lineCov">        294 :   g-&gt;gcstate = GCSswpallgc;</span>
<span class="lineNum">    1378 </span>            :   lua_assert(g-&gt;sweepgc == NULL);
<span class="lineNum">    1379 </span><span class="lineCov">        294 :   g-&gt;sweepgc = sweeptolive(L, &amp;g-&gt;allgc);</span>
<span class="lineNum">    1380 </span><span class="lineCov">        294 : }</span>
<span class="lineNum">    1381 </span>            : 
<span class="lineNum">    1382 </span>            : 
<span class="lineNum">    1383 </span>            : /*
<span class="lineNum">    1384 </span>            : ** Delete all objects in list 'p' until (but not including) object
<span class="lineNum">    1385 </span>            : ** 'limit'.
<span class="lineNum">    1386 </span>            : */
<span class="lineNum">    1387 </span><span class="lineCov">        282 : static void deletelist (lua_State *L, GCObject *p, GCObject *limit) {</span>
<span class="lineNum">    1388 </span><span class="lineCov">      40657 :   while (p != limit) {</span>
<span class="lineNum">    1389 </span><span class="lineCov">      40375 :     GCObject *next = p-&gt;next;</span>
<span class="lineNum">    1390 </span><span class="lineCov">      40375 :     freeobj(L, p);</span>
<span class="lineNum">    1391 </span><span class="lineCov">      40375 :     p = next;</span>
<span class="lineNum">    1392 </span>            :   }
<span class="lineNum">    1393 </span><span class="lineCov">        282 : }</span>
<span class="lineNum">    1394 </span>            : 
<span class="lineNum">    1395 </span>            : 
<span class="lineNum">    1396 </span>            : /*
<span class="lineNum">    1397 </span>            : ** Call all finalizers of the objects in the given Lua state, and
<span class="lineNum">    1398 </span>            : ** then free all objects, except for the main thread.
<span class="lineNum">    1399 </span>            : */
<span class="lineNum">    1400 </span><span class="lineCov">         94 : void luaC_freeallobjects (lua_State *L) {</span>
<span class="lineNum">    1401 </span><span class="lineCov">         94 :   global_State *g = G(L);</span>
<span class="lineNum">    1402 </span><span class="lineCov">         94 :   luaC_changemode(L, KGC_INC);</span>
<span class="lineNum">    1403 </span><span class="lineCov">         94 :   separatetobefnz(g, 1);  /* separate all objects with finalizers */</span>
<span class="lineNum">    1404 </span>            :   lua_assert(g-&gt;finobj == NULL);
<span class="lineNum">    1405 </span><span class="lineCov">         94 :   callallpendingfinalizers(L);</span>
<span class="lineNum">    1406 </span><span class="lineCov">         94 :   deletelist(L, g-&gt;allgc, obj2gco(g-&gt;mainthread));</span>
<span class="lineNum">    1407 </span><span class="lineCov">         94 :   deletelist(L, g-&gt;finobj, NULL);</span>
<span class="lineNum">    1408 </span><span class="lineCov">         94 :   deletelist(L, g-&gt;fixedgc, NULL);  /* collect fixed objects */</span>
<span class="lineNum">    1409 </span>            :   lua_assert(g-&gt;strt.nuse == 0);
<span class="lineNum">    1410 </span><span class="lineCov">         94 : }</span>
<span class="lineNum">    1411 </span>            : 
<span class="lineNum">    1412 </span>            : 
<span class="lineNum">    1413 </span><span class="lineCov">        618 : static lu_mem atomic (lua_State *L) {</span>
<span class="lineNum">    1414 </span><span class="lineCov">        618 :   global_State *g = G(L);</span>
<span class="lineNum">    1415 </span><span class="lineCov">        618 :   lu_mem work = 0;</span>
<span class="lineNum">    1416 </span>            :   GCObject *origweak, *origall;
<span class="lineNum">    1417 </span><span class="lineCov">        618 :   GCObject *grayagain = g-&gt;grayagain;  /* save original list */</span>
<span class="lineNum">    1418 </span><span class="lineCov">        618 :   g-&gt;grayagain = NULL;</span>
<span class="lineNum">    1419 </span>            :   lua_assert(g-&gt;ephemeron == NULL &amp;&amp; g-&gt;weak == NULL);
<span class="lineNum">    1420 </span>            :   lua_assert(!iswhite(g-&gt;mainthread));
<span class="lineNum">    1421 </span><span class="lineCov">        618 :   g-&gt;gcstate = GCSatomic;</span>
<span class="lineNum">    1422 </span><span class="lineCov">        618 :   markobject(g, L);  /* mark running thread */</span>
<span class="lineNum">    1423 </span>            :   /* registry and global metatables may be changed by API */
<span class="lineNum">    1424 </span><span class="lineCov">        618 :   markvalue(g, &amp;g-&gt;l_registry);</span>
<span class="lineNum">    1425 </span><span class="lineCov">        618 :   markmt(g);  /* mark global metatables */</span>
<span class="lineNum">    1426 </span><span class="lineCov">        618 :   work += propagateall(g);  /* empties 'gray' list */</span>
<span class="lineNum">    1427 </span>            :   /* remark occasional upvalues of (maybe) dead threads */
<span class="lineNum">    1428 </span><span class="lineCov">        618 :   work += remarkupvals(g);</span>
<span class="lineNum">    1429 </span><span class="lineCov">        618 :   work += propagateall(g);  /* propagate changes */</span>
<span class="lineNum">    1430 </span><span class="lineCov">        618 :   g-&gt;gray = grayagain;</span>
<span class="lineNum">    1431 </span><span class="lineCov">        618 :   work += propagateall(g);  /* traverse 'grayagain' list */</span>
<span class="lineNum">    1432 </span><span class="lineCov">        618 :   convergeephemerons(g);</span>
<span class="lineNum">    1433 </span>            :   /* at this point, all strongly accessible objects are marked. */
<span class="lineNum">    1434 </span>            :   /* Clear values from weak tables, before checking finalizers */
<span class="lineNum">    1435 </span><span class="lineCov">        618 :   clearbyvalues(g, g-&gt;weak, NULL);</span>
<span class="lineNum">    1436 </span><span class="lineCov">        618 :   clearbyvalues(g, g-&gt;allweak, NULL);</span>
<span class="lineNum">    1437 </span><span class="lineCov">        618 :   origweak = g-&gt;weak; origall = g-&gt;allweak;</span>
<span class="lineNum">    1438 </span><span class="lineCov">        618 :   separatetobefnz(g, 0);  /* separate objects to be finalized */</span>
<span class="lineNum">    1439 </span><span class="lineCov">        618 :   work += markbeingfnz(g);  /* mark objects that will be finalized */</span>
<span class="lineNum">    1440 </span><span class="lineCov">        618 :   work += propagateall(g);  /* remark, to propagate 'resurrection' */</span>
<span class="lineNum">    1441 </span><span class="lineCov">        618 :   convergeephemerons(g);</span>
<span class="lineNum">    1442 </span>            :   /* at this point, all resurrected objects are marked. */
<span class="lineNum">    1443 </span>            :   /* remove dead objects from weak tables */
<span class="lineNum">    1444 </span><span class="lineCov">        618 :   clearbykeys(g, g-&gt;ephemeron);  /* clear keys from all ephemeron tables */</span>
<span class="lineNum">    1445 </span><span class="lineCov">        618 :   clearbykeys(g, g-&gt;allweak);  /* clear keys from all 'allweak' tables */</span>
<span class="lineNum">    1446 </span>            :   /* clear values from resurrected weak tables */
<span class="lineNum">    1447 </span><span class="lineCov">        618 :   clearbyvalues(g, g-&gt;weak, origweak);</span>
<span class="lineNum">    1448 </span><span class="lineCov">        618 :   clearbyvalues(g, g-&gt;allweak, origall);</span>
<span class="lineNum">    1449 </span><span class="lineCov">        618 :   luaS_clearcache(g);</span>
<span class="lineNum">    1450 </span><span class="lineCov">        618 :   g-&gt;currentwhite = cast_byte(otherwhite(g));  /* flip current white */</span>
<span class="lineNum">    1451 </span>            :   lua_assert(g-&gt;gray == NULL);
<span class="lineNum">    1452 </span><span class="lineCov">        618 :   return work;  /* estimate of slots marked by 'atomic' */</span>
<span class="lineNum">    1453 </span>            : }
<span class="lineNum">    1454 </span>            : 
<span class="lineNum">    1455 </span>            : 
<span class="lineNum">    1456 </span><span class="lineCov">       1926 : static int sweepstep (lua_State *L, global_State *g,</span>
<span class="lineNum">    1457 </span>            :                       int nextstate, GCObject **nextlist) {
<span class="lineNum">    1458 </span><span class="lineCov">       1926 :   if (g-&gt;sweepgc) {</span>
<span class="lineNum">    1459 </span><span class="lineCov">       1044 :     l_mem olddebt = g-&gt;GCdebt;</span>
<span class="lineNum">    1460 </span>            :     int count;
<span class="lineNum">    1461 </span><span class="lineCov">       1044 :     g-&gt;sweepgc = sweeplist(L, g-&gt;sweepgc, GCSWEEPMAX, &amp;count);</span>
<span class="lineNum">    1462 </span><span class="lineCov">       1044 :     g-&gt;GCestimate += g-&gt;GCdebt - olddebt;  /* update estimate */</span>
<span class="lineNum">    1463 </span><span class="lineCov">       1044 :     return count;</span>
<span class="lineNum">    1464 </span>            :   }
<span class="lineNum">    1465 </span>            :   else {  /* enter next state */
<span class="lineNum">    1466 </span><span class="lineCov">        882 :     g-&gt;gcstate = nextstate;</span>
<span class="lineNum">    1467 </span><span class="lineCov">        882 :     g-&gt;sweepgc = nextlist;</span>
<span class="lineNum">    1468 </span><span class="lineCov">        882 :     return 0;  /* no work done */</span>
<span class="lineNum">    1469 </span>            :   }
<span class="lineNum">    1470 </span>            : }
<span class="lineNum">    1471 </span>            : 
<span class="lineNum">    1472 </span>            : 
<span class="lineNum">    1473 </span><span class="lineCov">       7762 : static lu_mem singlestep (lua_State *L) {</span>
<span class="lineNum">    1474 </span><span class="lineCov">       7762 :   global_State *g = G(L);</span>
<span class="lineNum">    1475 </span><span class="lineCov">       7762 :   switch (g-&gt;gcstate) {</span>
<span class="lineNum">    1476 </span><span class="lineCov">        436 :     case GCSpause: {</span>
<span class="lineNum">    1477 </span><span class="lineCov">        436 :       restartcollection(g);</span>
<span class="lineNum">    1478 </span><span class="lineCov">        436 :       g-&gt;gcstate = GCSpropagate;</span>
<span class="lineNum">    1479 </span><span class="lineCov">        436 :       return 1;</span>
<span class="lineNum">    1480 </span>            :     }
<span class="lineNum">    1481 </span><span class="lineCov">       4526 :     case GCSpropagate: {</span>
<span class="lineNum">    1482 </span><span class="lineCov">       4526 :       if (g-&gt;gray == NULL) {  /* no more gray objects? */</span>
<span class="lineNum">    1483 </span><span class="lineCov">        286 :         g-&gt;gcstate = GCSenteratomic;  /* finish propagate phase */</span>
<span class="lineNum">    1484 </span><span class="lineCov">        286 :         return 0;</span>
<span class="lineNum">    1485 </span>            :       }
<span class="lineNum">    1486 </span>            :       else
<span class="lineNum">    1487 </span><span class="lineCov">       4240 :         return propagatemark(g);  /* traverse one gray object */</span>
<span class="lineNum">    1488 </span>            :     }
<span class="lineNum">    1489 </span><span class="lineCov">        286 :     case GCSenteratomic: {</span>
<span class="lineNum">    1490 </span><span class="lineCov">        286 :       lu_mem work = atomic(L);  /* work is what was traversed by 'atomic' */</span>
<span class="lineNum">    1491 </span><span class="lineCov">        286 :       entersweep(L);</span>
<span class="lineNum">    1492 </span><span class="lineCov">        286 :       g-&gt;GCestimate = gettotalbytes(g);  /* first estimate */;</span>
<span class="lineNum">    1493 </span><span class="lineCov">        286 :       return work;</span>
<span class="lineNum">    1494 </span>            :     }
<span class="lineNum">    1495 </span><span class="lineCov">        750 :     case GCSswpallgc: {  /* sweep &quot;regular&quot; objects */</span>
<span class="lineNum">    1496 </span><span class="lineCov">        750 :       return sweepstep(L, g, GCSswpfinobj, &amp;g-&gt;finobj);</span>
<span class="lineNum">    1497 </span>            :     }
<span class="lineNum">    1498 </span><span class="lineCov">        588 :     case GCSswpfinobj: {  /* sweep objects with finalizers */</span>
<span class="lineNum">    1499 </span><span class="lineCov">        588 :       return sweepstep(L, g, GCSswptobefnz, &amp;g-&gt;tobefnz);</span>
<span class="lineNum">    1500 </span>            :     }
<span class="lineNum">    1501 </span><span class="lineCov">        588 :     case GCSswptobefnz: {  /* sweep objects to be finalized */</span>
<span class="lineNum">    1502 </span><span class="lineCov">        588 :       return sweepstep(L, g, GCSswpend, NULL);</span>
<span class="lineNum">    1503 </span>            :     }
<span class="lineNum">    1504 </span><span class="lineCov">        294 :     case GCSswpend: {  /* finish sweeps */</span>
<span class="lineNum">    1505 </span><span class="lineCov">        294 :       checkSizes(L, g);</span>
<span class="lineNum">    1506 </span><span class="lineCov">        294 :       g-&gt;gcstate = GCScallfin;</span>
<span class="lineNum">    1507 </span><span class="lineCov">        294 :       return 0;</span>
<span class="lineNum">    1508 </span>            :     }
<span class="lineNum">    1509 </span><span class="lineCov">        294 :     case GCScallfin: {  /* call remaining finalizers */</span>
<span class="lineNum">    1510 </span><span class="lineCov">        294 :       if (g-&gt;tobefnz &amp;&amp; !g-&gt;gcemergency) {</span>
<span class="lineNum">    1511 </span><span class="lineNoCov">          0 :         int n = runafewfinalizers(L, GCFINMAX);</span>
<span class="lineNum">    1512 </span><span class="lineNoCov">          0 :         return n * GCFINALIZECOST;</span>
<span class="lineNum">    1513 </span>            :       }
<span class="lineNum">    1514 </span>            :       else {  /* emergency mode or no more finalizers */
<span class="lineNum">    1515 </span><span class="lineCov">        294 :         g-&gt;gcstate = GCSpause;  /* finish collection */</span>
<span class="lineNum">    1516 </span><span class="lineCov">        294 :         return 0;</span>
<span class="lineNum">    1517 </span>            :       }
<span class="lineNum">    1518 </span>            :     }
<span class="lineNum">    1519 </span><span class="lineNoCov">          0 :     default: lua_assert(0); return 0;</span>
<span class="lineNum">    1520 </span>            :   }
<span class="lineNum">    1521 </span>            : }
<span class="lineNum">    1522 </span>            : 
<span class="lineNum">    1523 </span>            : 
<span class="lineNum">    1524 </span>            : /*
<span class="lineNum">    1525 </span>            : ** advances the garbage collector until it reaches a state allowed
<span class="lineNum">    1526 </span>            : ** by 'statemask'
<span class="lineNum">    1527 </span>            : */
<span class="lineNum">    1528 </span><span class="lineCov">        292 : void luaC_runtilstate (lua_State *L, int statesmask) {</span>
<span class="lineNum">    1529 </span><span class="lineCov">        292 :   global_State *g = G(L);</span>
<span class="lineNum">    1530 </span><span class="lineCov">        579 :   while (!testbit(statesmask, g-&gt;gcstate))</span>
<span class="lineNum">    1531 </span><span class="lineCov">        287 :     singlestep(L);</span>
<span class="lineNum">    1532 </span><span class="lineCov">        292 : }</span>
<span class="lineNum">    1533 </span>            : 
<span class="lineNum">    1534 </span>            : 
<span class="lineNum">    1535 </span>            : /*
<span class="lineNum">    1536 </span>            : ** Performs a basic incremental step. The debt and step size are
<span class="lineNum">    1537 </span>            : ** converted from bytes to &quot;units of work&quot;; then the function loops
<span class="lineNum">    1538 </span>            : ** running single steps until adding that many units of work or
<span class="lineNum">    1539 </span>            : ** finishing a cycle (pause state). Finally, it sets the debt that
<span class="lineNum">    1540 </span>            : ** controls when next step will be performed.
<span class="lineNum">    1541 </span>            : */
<span class="lineNum">    1542 </span><span class="lineCov">        286 : static void incstep (lua_State *L, global_State *g) {</span>
<span class="lineNum">    1543 </span><span class="lineCov">        286 :   int stepmul = (getgcparam(g-&gt;gcstepmul) | 1);  /* avoid division by 0 */</span>
<span class="lineNum">    1544 </span><span class="lineCov">        286 :   l_mem debt = (g-&gt;GCdebt / WORK2MEM) * stepmul;</span>
<span class="lineNum">    1545 </span><span class="lineCov">        286 :   l_mem stepsize = (g-&gt;gcstepsize &lt;= log2maxs(l_mem))</span>
<span class="lineNum">    1546 </span><span class="lineCov">        286 :                  ? ((cast(l_mem, 1) &lt;&lt; g-&gt;gcstepsize) / WORK2MEM) * stepmul</span>
<span class="lineNum">    1547 </span><span class="lineCov">        286 :                  : MAX_LMEM;  /* overflow; keep maximum value */</span>
<span class="lineNum">    1548 </span>            :   do {  /* repeat until pause or enough &quot;credit&quot; (negative debt) */
<span class="lineNum">    1549 </span><span class="lineCov">       7475 :     lu_mem work = singlestep(L);  /* perform one single step */</span>
<span class="lineNum">    1550 </span><span class="lineCov">       7475 :     debt -= work;</span>
<span class="lineNum">    1551 </span><span class="lineCov">       7475 :   } while (debt &gt; -stepsize &amp;&amp; g-&gt;gcstate != GCSpause);</span>
<span class="lineNum">    1552 </span><span class="lineCov">        286 :   if (g-&gt;gcstate == GCSpause)</span>
<span class="lineNum">    1553 </span><span class="lineCov">        286 :     setpause(g);  /* pause until next cycle */</span>
<span class="lineNum">    1554 </span>            :   else {
<span class="lineNum">    1555 </span><span class="lineNoCov">          0 :     debt = (debt / stepmul) * WORK2MEM;  /* convert 'work units' to bytes */</span>
<span class="lineNum">    1556 </span><span class="lineNoCov">          0 :     luaE_setdebt(g, debt);</span>
<span class="lineNum">    1557 </span>            :   }
<span class="lineNum">    1558 </span><span class="lineCov">        286 : }</span>
<span class="lineNum">    1559 </span>            : 
<span class="lineNum">    1560 </span>            : /*
<span class="lineNum">    1561 </span>            : ** performs a basic GC step if collector is running
<span class="lineNum">    1562 </span>            : */
<span class="lineNum">    1563 </span><span class="lineCov">        525 : void luaC_step (lua_State *L) {</span>
<span class="lineNum">    1564 </span><span class="lineCov">        525 :   global_State *g = G(L);</span>
<span class="lineNum">    1565 </span><span class="lineCov">        525 :   if (g-&gt;gcrunning) {  /* running? */</span>
<span class="lineNum">    1566 </span><span class="lineCov">        525 :     if(isdecGCmodegen(g))</span>
<span class="lineNum">    1567 </span><span class="lineCov">        239 :       genstep(L, g);</span>
<span class="lineNum">    1568 </span>            :     else
<span class="lineNum">    1569 </span><span class="lineCov">        286 :       incstep(L, g);</span>
<span class="lineNum">    1570 </span>            :   }
<span class="lineNum">    1571 </span><span class="lineCov">        525 : }</span>
<span class="lineNum">    1572 </span>            : 
<span class="lineNum">    1573 </span>            : 
<span class="lineNum">    1574 </span>            : /*
<span class="lineNum">    1575 </span>            : ** Perform a full collection in incremental mode.
<span class="lineNum">    1576 </span>            : ** Before running the collection, check 'keepinvariant'; if it is true,
<span class="lineNum">    1577 </span>            : ** there may be some objects marked as black, so the collector has
<span class="lineNum">    1578 </span>            : ** to sweep all objects to turn them back to white (as white has not
<span class="lineNum">    1579 </span>            : ** changed, nothing will be collected).
<span class="lineNum">    1580 </span>            : */
<span class="lineNum">    1581 </span><span class="lineNoCov">          0 : static void fullinc (lua_State *L, global_State *g) {</span>
<span class="lineNum">    1582 </span><span class="lineNoCov">          0 :   if (keepinvariant(g))  /* black objects? */</span>
<span class="lineNum">    1583 </span><span class="lineNoCov">          0 :     entersweep(L); /* sweep everything to turn them back to white */</span>
<span class="lineNum">    1584 </span>            :   /* finish any pending sweep phase to start a new cycle */
<span class="lineNum">    1585 </span><span class="lineNoCov">          0 :   luaC_runtilstate(L, bitmask(GCSpause));</span>
<span class="lineNum">    1586 </span><span class="lineNoCov">          0 :   luaC_runtilstate(L, bitmask(GCScallfin));  /* run up to finalizers */</span>
<span class="lineNum">    1587 </span>            :   /* estimate must be correct after a full GC cycle */
<span class="lineNum">    1588 </span>            :   lua_assert(g-&gt;GCestimate == gettotalbytes(g));
<span class="lineNum">    1589 </span><span class="lineNoCov">          0 :   luaC_runtilstate(L, bitmask(GCSpause));  /* finish collection */</span>
<span class="lineNum">    1590 </span><span class="lineNoCov">          0 :   setpause(g);</span>
<span class="lineNum">    1591 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">    1592 </span>            : 
<span class="lineNum">    1593 </span>            : 
<span class="lineNum">    1594 </span>            : /*
<span class="lineNum">    1595 </span>            : ** Performs a full GC cycle; if 'isemergency', set a flag to avoid
<span class="lineNum">    1596 </span>            : ** some operations which could change the interpreter state in some
<span class="lineNum">    1597 </span>            : ** unexpected ways (running finalizers and shrinking some structures).
<span class="lineNum">    1598 </span>            : */
<span class="lineNum">    1599 </span><span class="lineCov">          2 : void luaC_fullgc (lua_State *L, int isemergency) {</span>
<span class="lineNum">    1600 </span><span class="lineCov">          2 :   global_State *g = G(L);</span>
<span class="lineNum">    1601 </span>            :   lua_assert(!g-&gt;gcemergency);
<span class="lineNum">    1602 </span><span class="lineCov">          2 :   g-&gt;gcemergency = isemergency;  /* set flag */</span>
<span class="lineNum">    1603 </span><span class="lineCov">          2 :   if (g-&gt;gckind == KGC_INC)</span>
<span class="lineNum">    1604 </span><span class="lineNoCov">          0 :     fullinc(L, g);</span>
<span class="lineNum">    1605 </span>            :   else
<span class="lineNum">    1606 </span><span class="lineCov">          2 :     fullgen(L, g);</span>
<span class="lineNum">    1607 </span><span class="lineCov">          2 :   g-&gt;gcemergency = 0;</span>
<span class="lineNum">    1608 </span><span class="lineCov">          2 : }</span>
<span class="lineNum">    1609 </span>            : 
<span class="lineNum">    1610 </span>            : /* }====================================================== */
<span class="lineNum">    1611 </span>            : 
<span class="lineNum">    1612 </span>            : 
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.13</a></td></tr>
  </table>
  <br>

</body>
</html>
